An overview of the
You can also create a fragment or array of bool data type in Golang. In fact, fragments or arrays of any data type can be created in Go. This tutorial contains simple examples of creating a shard or array of bool data type in Golang.
It should be added here that in Golang, arrays are fixed size, while fragments can have variable size. More details here
Array – https://golangbyexample.com/understanding-array-golang-complete-guide/
Slice – https://golangbyexample.com/slice-in-golang/
Fragments of a Bool
package main
import "fmt"
func main(a) {
//First Way
var booleans_first []bool
booleans_first = append(booleans_first, true)
booleans_first = append(booleans_first, false)
booleans_first = append(booleans_first, true)
fmt.Println("Output for First slice of booleans")
for _, c := range booleans_first {
fmt.Println(c)
}
//Second Way
booleans_second := make([]bool.3)
booleans_second[0] = false
booleans_second[1] = true
booleans_second[2] = false
fmt.Println("\nOutput for Second slice of booleans")
for _, c := range booleans_second {
fmt.Println(c)
}
}
Copy the code
The output
Output for First slice of booleans
true
false
true
Output for Second slice of booleans
false
true
false
Copy the code
There are two ways we can create a slice of bool. The first way is
var booleans_first []bool
booleans_first = append(booleans_first, true)
booleans_first = append(booleans_first, false)
booleans_first = append(booleans_first, true)
Copy the code
In the second way, we use the make command to create a slice of bool
booleans_second := make([]bool.3)
booleans_second[0] = false
booleans_second[1] = true
booleans_second[2] = false
Copy the code
Either way. So that’s how we create a fragment of bool, right
Boolean array
package main
import "fmt"
func main(a) {
var booleans_first [3]bool
booleans_first[0] = true
booleans_first[1] = false
booleans_first[2] = true
fmt.Println("Output for First Array of booleans")
for _, c := range booleans_first {
fmt.Println(c)
}
booleans_second := [3]bool{
false.true.false,
}
fmt.Println("\nOutput for Second Array of booleans")
for _, c := range booleans_second {
fmt.Println(c)
}
}
Copy the code
The output
Output for First Array of booleans
true
false
true
Output for Second Array of booleans
false
true
false
Copy the code
There are two ways to create arrays. The first way is
var booleans_first [3]bool
booleans_first[0] = true
booleans_first[1] = false
booleans_first[2] = true
Copy the code
In the second way, we initialize the array directly with the created Boolean value
booleans_second := [3]bool{
false.true.false,}Copy the code
Take a look at our advanced Golang tutorial. This series of tutorials is carefully designed and we try to cover all the concepts with examples. This tutorial is for those who want to gain professional knowledge and a solid understanding of Golang – Golang Advanced Tutorial
If you are interested in learning how to implement all design patterns in Golang. If so, then this article is for you — All Design Patterns Golang
The postSlice or Array of Bool in Go (Golang)appeared first onWelcome To Golang By Example.