循环遍历一个结构体切片时,结构体的修改以及传递是怎样的?

    1. package main
    2. import "fmt"
    3. type Person struct {
    4. Name string
    5. }
    6. func main() {
    7. persons := []Person{
    8. {Name: "John"},
    9. {Name: "Jane"},
    10. {Name: "Jack"},
    11. }
    12. var persons2 []Person
    13. for i, person := range persons {
    14. fmt.Printf("loop person addr: %p, name: %s\n", &person, person.Name)
    15. fmt.Printf("original person addr: %p, name: %s\n", &persons[i], persons[i].Name)
    16. persons2 = append(persons2, person)
    17. }
    18. for i := range persons2 {
    19. fmt.Printf("new person addr: %p, name: %s\n", &persons2[i], persons2[i].Name)
    20. }
    21. }

    image.png