Skip to content

Instantly share code, notes, and snippets.

@r002
Created June 8, 2021 17:26
Show Gist options
  • Save r002/4d998976c8c9509b5546d44655b1af4c to your computer and use it in GitHub Desktop.
Save r002/4d998976c8c9509b5546d44655b1af4c to your computer and use it in GitHub Desktop.
Go Tutorial: Array example - Always passed by value
func TestHello(t *testing.T) {
fmt.Println(">> Array example - Always passed by value")
testcase1 := [1]struct {
arg string
want string
}{
{"original arg val", "original want val"},
}
fmt.Println(">> testcase1.arg:", testcase1[0].arg)
testcase2 := testcase1
testcase1[0].arg = "changed arg val"
fmt.Println(">> testcase2.arg:", testcase2[0].arg)
}
// Output:
>> Array example - Always passed by value
>> testcase1.arg: original arg val
>> testcase2.arg: original arg val
@r002
Copy link
Author

r002 commented Jun 8, 2021

https://blog.golang.org/slices-intro

Observe that:

testcase1 := [...]struct {
	arg  string
	want string
}{
	{"original arg val", "original want val"},
}

is the same as:

testcase1 := [1]struct {
	arg  string
	want string
}{
	{"original arg val", "original want val"},
}

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