Last active
January 20, 2020 09:32
-
-
Save kjk/83c99d94cbd25e6fd6ea184a2432a4c8 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 Foo struct { | |
Str string | |
} | |
func (f Foo) Print() { | |
fmt.Printf("Value of Str inside Print: '%s'\n", f.Str) | |
fmt.Printf("Address of &f inside Print: 0x%p\n", &f) | |
f.Str = "changed" | |
} | |
func main() { | |
fv := Foo{ | |
Str: "Foo", | |
} | |
fv.Print() | |
fmt.Printf("Address of &fv before Print: 0x%p\n", &fv); | |
fmt.Printf("Value of Str after Print: '%s'\n", fv.Str) | |
} | |
// :show end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment