Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active January 20, 2020 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/f225654f7c0177efd2871978f988a06d to your computer and use it in GitHub Desktop.
Save kjk/f225654f7c0177efd2871978f988a06d to your computer and use it in GitHub Desktop.
Value vs. pointer receiver (made with https://codeeval.dev)
// :collection Essential Go
package main
import "fmt"
// :show start
type Person struct {
FirstName string
LastName string
}
func (p Person) PrintFullNameValue() {
fmt.Printf("PrintFullNameValue: address of p is %p\n", &p)
}
func (p *Person) PrintFullNamePointer() {
fmt.Printf("PrintFullNamePointer: p is %p\n", p)
}
func main() {
p := Person{
"John",
"Doe",
}
fmt.Printf("address of p: %p\n", &p)
p.PrintFullNamePointer()
p.PrintFullNameValue()
pp := &p
fmt.Printf("\naddress of pp: %p\n", pp)
pp.PrintFullNamePointer()
pp.PrintFullNameValue()
}
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment