在 golang 中循环 struts 的数组
loop over a array of struts in golang
我的术语可能不适用,所以我使用了一些 python 个术语。
在 golang 中尝试做的是迭代具有这样存储值的支柱(这是我从 api 得到的)
[{STOREA 0 0 0} {STOREB 0 0 0} {STOREC 0 0 0} {STORED 0 0 0}]
在 python 中,我将其称为听写列表。
当我将鼠标悬停在可视代码中的值上时,它指出:
field itemData []struct{Code string "json:"Code""; Items int
"json:"Items""; Price float64 "json:"Price""; Qty int
"json:"Qty""}
package main
// I think this is the right way to interpret what visual code stated
type itemData struct {
Code string `json:"Code"`
Items int `json:"Items"`
Price float64 `json:"Price"`
Qty int `json:"Qty"`
}
//I'm trying to simulate the api response by storing it in a varible like this but I think the [] are an issue and im doing it incorrectly
var storeData = itemData[{STOREA 0 0 0} {STOREB0 0 0} {STOREC 0 0 0} {STORED0 0 0}] // I think the [ ] brackets are causing an issue
//The idea is I can iterate over it like this:
for k,v := range storeData {
fmt.Printf(k,v)
}
然后试试这个:
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)
}
您得到的是 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)
}
我的术语可能不适用,所以我使用了一些 python 个术语。
在 golang 中尝试做的是迭代具有这样存储值的支柱(这是我从 api 得到的)
[{STOREA 0 0 0} {STOREB 0 0 0} {STOREC 0 0 0} {STORED 0 0 0}]
在 python 中,我将其称为听写列表。
当我将鼠标悬停在可视代码中的值上时,它指出:
field itemData []struct{Code string "json:"Code""; Items int "json:"Items""; Price float64 "json:"Price""; Qty int "json:"Qty""}
package main
// I think this is the right way to interpret what visual code stated
type itemData struct {
Code string `json:"Code"`
Items int `json:"Items"`
Price float64 `json:"Price"`
Qty int `json:"Qty"`
}
//I'm trying to simulate the api response by storing it in a varible like this but I think the [] are an issue and im doing it incorrectly
var storeData = itemData[{STOREA 0 0 0} {STOREB0 0 0} {STOREC 0 0 0} {STORED0 0 0}] // I think the [ ] brackets are causing an issue
//The idea is I can iterate over it like this:
for k,v := range storeData {
fmt.Printf(k,v)
}
然后试试这个:
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)
}
您得到的是 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)
}