Last active
January 20, 2020 09:21
-
-
Save kjk/f225654f7c0177efd2871978f988a06d to your computer and use it in GitHub Desktop.
Value vs. pointer receiver (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// :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