Values and pointers: What could possibly go wrong?
This post highlights a weird and expected behavior we encountered while using Go’s encoding/json library. In go, we pass by value, that really means this: type User struct { Name string } func changeMe(u User) { u.Name = "New Name" } func main(){ u := User{Name: "mohamed"} changeMe(u) log.Printf("The user is: %s", u.Name) // mohamed } To change by value, we must pass a pointer, we can change the code to be something like this: func changeMe(u *User) { u.Name = "New Name" } // same code u := User{Name: "mohamed"} changeMe(&u) //<--- we made a change here! We should pass the memory address to user log.Printf("The user is: %s", u.Name) // New Name But in go, we slightly use a different syntax for this, we use a method receiver on the struct. More like method to classes on other languages. ...