create a pointer to a bool in go

To create a pointer to a bool in Go, you need to first create a bool variable and then create a pointer to that variable using the address-of operator '&'.

main.go
package main

import "fmt"

func main() {
    var b bool = true
    var ptr *bool = &b
    
    fmt.Println(b)     // prints: true
    fmt.Println(*ptr)  // prints: true
}
172 chars
12 lines

In the above code snippet, we first create a bool variable b and initialize it to true. We then create a pointer variable ptr of type *bool, and assign it the memory address of b using the address-of operator '&'.

To access the value stored in the bool variable using the pointer, we use the dereference operator *. In the fmt.Println(*ptr) statement, we print the value of b that is stored at the memory location pointed to by ptr.

related categories

gistlibby LogSnag