package main
import (
"fmt"
)
type test struct {
Name string
}
func main() {
foo := []test{
{Name: "a"},
{Name: "b"},
}
var bar []*test
for _, item := range foo {
fmt.Printf("%v\n", item.Name)
bar = append(bar, &item)
}
for _, item := range bar {
fmt.Printf("%v\n", item.Name)
}
}
When I run the code above, I expect to see the output
a
b
a
b
However, the output is
a
b
b
b
What's going on?
For reference, this was done in the go playground