create a struct with a field of type bool in go

To create a struct in Go with a field of type bool, define the struct type and add a field with the bool type as follows:

main.go
type MyStruct struct {
    myBoolField bool
}
46 chars
4 lines

Here, MyStruct is the name of the struct, and myBoolField is the name of the bool field within MyStruct.

You can then create an instance of MyStruct and set myBoolField using the following code:

main.go
myStruct := MyStruct{myBoolField: true}
40 chars
2 lines

or

main.go
myStruct := MyStruct{}
myStruct.myBoolField = true
51 chars
3 lines

Here, myStruct is an instance of MyStruct with myBoolField set to true.

related categories

gistlibby LogSnag