Skip to content

Instantly share code, notes, and snippets.

@r002
Last active June 8, 2021 17:14
Show Gist options
  • Save r002/eee3b89181441f86710ea23f85fcff25 to your computer and use it in GitHub Desktop.
Save r002/eee3b89181441f86710ea23f85fcff25 to your computer and use it in GitHub Desktop.
Go Tutorial: & - Pass by Reference
// https://stackoverflow.com/questions/33242850/in-golang-what-is-the-difference-between-and
type TestCase struct {
arg string
want string
}
func TestHello(t *testing.T) {
testcase1 := &TestCase{
arg: "original arg val",
want: "original want val",
}
fmt.Println(">> testcase1.arg:", testcase1.arg)
testcase2 := testcase1
testcase1.arg = "changed arg val"
fmt.Println(">> testcase2.arg:", testcase2.arg)
}
// Output:
>> testcase1.arg: original arg val
>> testcase2.arg: changed arg val
@r002
Copy link
Author

r002 commented Jun 8, 2021

Observe:

testcase1 := &TestCase{
	arg:  "original arg val",
	want: "original want val",
}

is the same as:

testcase1 := &TestCase{"original arg val", "original want val"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment