I'm trying to figure out how pointers work in Go and I think I'm starting to get it, but this is confusing me and I don't really know what to search for. Let's say I have the following function:
func createNode(nodeInfo string) *TreeNode {
return &TreeNode{info: nodeInfo}
}
I understand that the function is returning the memory address of the created struct instance, but how does the function signature say *TreeNode
? According to my understanding, the *
is used to dereference pointers to get the value itself, so what is happening here?
Also, here:
func zero(xPtr *int) {
*xPtr = 0
}
func main() {
x := 5
zero(&x)
}
The opposite is happening where the function is accepting an argument with the *
operator but the function itself is being called with &
operator.