Skip to content

Instantly share code, notes, and snippets.

@timruffles
Last active August 19, 2019 07:43
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 timruffles/5bc17fd7dbd644130001397c3b365394 to your computer and use it in GitHub Desktop.
Save timruffles/5bc17fd7dbd644130001397c3b365394 to your computer and use it in GitHub Desktop.
What does this program output, and why? Reason it out gophers! Answers in a spoiler block - https://github.com/dear-github/dear-github/issues/166#issuecomment-236342209
package main
import "fmt"
func main() {
type person struct {
nickname string
}
ppl := []person{
{
nickname: "the knife",
},
{
nickname: "super hans",
},
}
nicknames := make([]*string, 0)
for _, p := range ppl {
nicknames = append(nicknames, &p.nickname)
}
fmt.Println(*nicknames[0], *nicknames[1])
}
@rjinski
Copy link

rjinski commented May 28, 2019

Is the link to the answer a typo Tim?

@rjinski
Copy link

rjinski commented May 28, 2019

I had to run it; and I was initially wrong, but, I've assume for _, p := range ppl creates a copy of ppl at a new pointer...?

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