Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active January 20, 2020 09:32
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/83c99d94cbd25e6fd6ea184a2432a4c8 to your computer and use it in GitHub Desktop.
Save kjk/83c99d94cbd25e6fd6ea184a2432a4c8 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 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