Skip to content

Instantly share code, notes, and snippets.

@p886
Created January 23, 2015 16:32
Show Gist options
  • Save p886/44d73618ff04d2afb3a7 to your computer and use it in GitHub Desktop.
Save p886/44d73618ff04d2afb3a7 to your computer and use it in GitHub Desktop.
Basic Understanding of Pointers in Go
package main
import "fmt"
// Explanation:
// changeIt() takes as argument a pointer to a thing
// in line 14 we create such a pointer and pass it to changeIt().
// Hadn't we used a pointer but instead passed thing *normally*,
// we would not have been able to observe the change
// from "yolo" to "roflmao" in main
func main() {
thing := Thing{name: "yolo"}
changeIt(&thing)
fmt.Println(thing.name)
}
type Thing struct {
name string
}
func changeIt(thing *Thing) {
thing.name = "roflmao"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment