英文:
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中,我会称之为字典列表。
当我在Visual Studio Code中悬停在值上时,它显示如下:
field itemData []struct{Code string "json:"Code""; Items int "json:"Items""; Price float64 "json:"Price""; Qty int "json:"Qty""}
package main
// 我认为这是正确解释Visual Studio Code所述的方式
type itemData struct {
Code string `json:"Code"`
Items int `json:"Items"`
Price float64 `json:"Price"`
Qty int `json:"Qty"`
}
// 我试图通过将其存储在一个变量中来模拟API的响应,但我认为[]是个问题,我做得不正确
var storeData = itemData[{STOREA 0 0 0} {STOREB0 0 0} {STOREC 0 0 0} {STORED0 0 0}] // 我认为 [ ] 括号引起了问题
// 我的想法是可以这样迭代它:
for k, v := range storeData {
fmt.Printf(k, v)
}
英文:
My terminology maybe off so I'm using some python terms.
What trying to do in golang is iterate over a strut that is has a stored value like this (which is what I get back from the api)
[{STOREA 0 0 0} {STOREB 0 0 0} {STOREC 0 0 0} {STORED 0 0 0}]
In python I would call this a list of dicts.
When I hover over the value in visual code it states this:
> 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)
}
答案1
得分: 1
推荐阅读A Tour of 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
英文:
Recommended to give A Tour of Go - Slice
Then Try this:
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)
}
答案2
得分: 1
你所拥有的是itemData
(结构体)的一个切片。使用切片字面量来初始化切片并赋予值非常简单。代码如下所示:
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)
}
这段代码使用切片字面量创建了一个名为storeData
的切片,并初始化了其中的值。然后使用range
循环遍历切片,并打印每个元素的索引和值。
英文:
What you've is a slice
of itemData
(struct
). It's pretty straight forward to use a slice literal to initialize the slice with values. It looks like this:
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)
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论