2 回答
TA贡献1828条经验 获得超6个赞
推荐给Go 之旅 - Slice
然后试试这个:
var storeData = []itemData{{"STOREA", 0, 0, 0}, {"STOREB", 0, 0, 0}, {"STOREC", 0, 0, 0}, {"STORED", 0, 0, 0}}
for k, v := range storeData {
fmt.Println(k, v)
}
https://go.dev/play/p/aRaJlqKe00L
TA贡献1807条经验 获得超9个赞
你所拥有的slice是itemData( struct)。使用切片文字用值初始化切片非常简单。它看起来像这样:
storeData := []itemData{
{
Code: "STOREA",
Items: 0,
Price: 0,
Qty: 0,
},
{
Code: "STOREB0",
Items: 0,
Price: 0,
},
{
Code: "STOREC",
Items: 0,
Price: 0,
Qty: 0,
},
{
Code: "STORED0",
Items: 0,
Price: 0,
},
}
for i, v := range storeData {
fmt.Printf("index: %d, value: %v\n", i, v)
}
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报