Skip to content

Instantly share code, notes, and snippets.

@pjvds
Created July 9, 2013 19:12
Show Gist options
  • Save pjvds/5960326 to your computer and use it in GitHub Desktop.
Save pjvds/5960326 to your computer and use it in GitHub Desktop.
package main
type Foo struct {
Bar string
}
// It does update Bar value for receiving struct
// since it receives an pointer value.
func (foo *Foo) ChangeBarPtr(bar string) {
foo.Bar = bar
println("inmethod foo.Bar: " + foo.Bar)
}
// Does not update Bar value for receiving struct
// since it receives a copy.
func (foo Foo) ChangeBarValue(bar string) {
foo.Bar = bar
println("inmethod foo.Bar: " + foo.Bar)
}
func main() {
foo := Foo{
Bar: "Bar 0",
}
println("foo.Bar: " + foo.Bar)
foo.ChangeBarPtr("Bar 1")
println("foo.Bar: " + foo.Bar)
foo.ChangeBarValue("Bar 2")
println("foo.Bar: " + foo.Bar)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment