循环遍历一个结构体切片时,结构体的修改以及传递是怎样的?
package main
import "fmt"
type Person struct {
Name string
}
func main() {
persons := []Person{
{Name: "John"},
{Name: "Jane"},
{Name: "Jack"},
}
var persons2 []Person
for i, person := range persons {
fmt.Printf("loop person addr: %p, name: %s\n", &person, person.Name)
fmt.Printf("original person addr: %p, name: %s\n", &persons[i], persons[i].Name)
persons2 = append(persons2, person)
}
for i := range persons2 {
fmt.Printf("new person addr: %p, name: %s\n", &persons2[i], persons2[i].Name)
}
}