I am trying to wrap my head around pointer in go. I have this code right here
package main
import (
"fmt"
)
// LinkedList type
type LinkedList struct {
data int
next *LinkedList
}
// InsertList will insert a item into the list
func (node *LinkedList) InsertList(data int) {
newHead := LinkedList{data, node}
node = &newHead
}
func main() {
node := &LinkedList{}
node.InsertList(4)
fmt.Printf("node = %+v
", node)
}
and The output is
node = &{data:0 next:<nil>}
I would like to understand that why is node = &newHead
my InsertList method did not reference the node pointer to a different struct at all