1、案例1
package main import "fmt" type Card struct { id int } func main() { list := make([]*Card, 0) card := &Card{} for index := 1; index < 10; index++ { card.id = index list = append(list, card) } //fmt.Println(list) for _, c := range list { fmt.Println(*c) } }
输出结果:
2、案例2
package main import "fmt" type Card struct { id int } func main() { list := make([]*Card, 0) card := &Card{} for index := 1; index < 10; index++ { card = &Card{ id: index, } list = append(list, card) } for _, c := range list { fmt.Println("Card", c) } }
输出结果:
3、案例3
package main import "fmt" type Card struct { id int } func main() { list := make([]*Card, 0) for index := 1; index < 10; index++ { card := &Card{} card.id = index list = append(list, card) } for _, c := range list { fmt.Println("Card", c) } }
输出结果:
标签:index,语言,fmt,list,循环,card,go,id,Card From: https://www.cnblogs.com/wuchangblog/p/17049052.html