Created
July 9, 2013 19:12
-
-
Save pjvds/5960326 to your computer and use it in GitHub Desktop.
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
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