16

I'm trying to assign a value to a struct member that is a pointer, but it gives "panic: runtime error: invalid memory address or nil pointer dereference" at runtime...

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    var strctTest stctTest
    *strctTest.blTest = false

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

}

The runtime error seems to come from the assignment of the value with *strctTest.blTest = false , but why? How do I set it to false?

1

4 Answers 4

18

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))

}
Sign up to request clarification or add additional context in comments.

3 Comments

My attempt to try the first method gave a compiler error ("invalid pointer type *bool for composite literal") but the second method worked for my purposes. Thank you!
@BartSilverstrim Ah well I didn't test it. I guess I should have.
@BartSilverstrim I fixed my first example.
4

You could also do something like:

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := stctTest{
        blTest: &[]bool{true}[0],
    }

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

}

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

1 Comment

this is exactly what I was looking for .. Thanks :)
1

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.

2 Comments

This didn't really answer the OP's question. He asked how to assign a value to a bool, not how to make a new memory allocation. In your example you should add how to assign a value to it. See play.golang.org/p/iqOD5hLcPPp
If you're using a bool pointer instead of a bool, it's hopefully because you want to support three values true/false/nil, not 2 with default.
0

Following up on JTs Answer, I also would recommend using the new function as such:

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := &stctTest{
        blTest: new(bool),
    }

    *strctTest.blTest = true

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

After initializing the memory with new(), you can directly assign a value to the de-referenced pointer. This way you do not need to use another variable to get the address from.

https://go.dev/play/p/BmekoTalQVh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.