Golang: Assigning a value to struct member that is a pointer

Another way you can think of it is the zero value of a boolean is false.

This is not as clear but another way to do it.

https://play.golang.org/p/REbnJumcFi

I would recommend a New() func that returns a reference to a initialized struct type.


Why is it an error? Because a pointer only points. It doesn't create anything to point AT. You need to do that.

How to set it to false? This all depends on WHY you made it a pointer.

Is every copy of this supposed to point to the same bool? Then it should be allocated some space in a creation function.

func NewStruct() *strctTest {
    bl := true
    return &strctTest{
        blTest: &bl,
     }
}

Is the user supposed to point it at a boolean of his own? Then it should be set manually when creating the object.

func main() {
    myBool := false
    stctTest := strctTest{
        blTest: &myBool
    }

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))

}