Skip to content

Instantly share code, notes, and snippets.

@cevaris
Created March 1, 2015 22:17
Show Gist options
  • Save cevaris/d24d97f07a2ec638fa0f to your computer and use it in GitHub Desktop.
Save cevaris/d24d97f07a2ec638fa0f to your computer and use it in GitHub Desktop.
Golang Pointers
package main
// Run this in playground
// https://play.golang.org/p/k47thicjzn
import "fmt"
type Item struct {
a *Item
b string
}
func main() {
var i *Item = &Item{a:&Item{}, b:"pointer"}
var j *Item = i
fmt.Printf("%v %v %p %p %v\n", i, j, i, j, i==j)
i.b = "hell!!"
fmt.Printf("%v %v %p %p %v\n", i, j, i, j, i==j)
i.a = i
fmt.Printf("%v %v %p %p %v\n", i, j, i, j, i==j)
}
@cevaris
Copy link
Author

cevaris commented Mar 1, 2015

Output

&{0x10436190 pointer} &{0x10436190 pointer} 0x10436180 0x10436180 true
&{0x10436190 hell!!} &{0x10436190 hell!!} 0x10436180 0x10436180 true
&{0x10436180 hell!!} &{0x10436180 hell!!} 0x10436180 0x10436180 true

Source: How do I print the pointer value of a Go object? What does the pointer value mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment