英文:
golang, type int does not support indexing
问题
我在代码的一部分遇到了问题。我正在使用revel框架进行编写(为了清楚起见)。这是一个Worker go例程,我希望它能完成几件事情:
-
根据来源,切换stat变量的结构类型。我使用了一个switch语句,但在所有其他代码正确之前,我不确定switch语句是否编写正确。
-
我获取日期的缓存,并将其放入新的Work项目中。
-
我将Work发送到通道。
以下是我目前的代码:
func worker(in <-chan Task, out chan<- Work, wg *sync.WaitGroup) {
for t := range in {
for sourceName, charts := range t.Request.Charts {
var stat interface{}
switch sourceName {
case "noagg":
stat = stat.([]NoaggModel)
case "oracle":
stat = stat.([]OracleModel)
default:
panic("Invalid type for Work model!")
}
w := Work{Name:"", Data:""}
err := cache.Get(string(sourceName)+"_"+string(t.Date), &stat);
for chart := range charts{
w.Name = chart["name"]
if err == nil{
w.Data = countDataByName( stat, t.Request.Filters, string(chart["name"]))
}
out <- w
}
}
}
wg.Done() // this worker is now done; let the WaitGroup know.
}
但是现在我得到了错误消息invalid operation: chart["name"] (type int does not support indexing)
。
但是我有以下结构体:
type Chart struct {
Name string `json:"name"`
Type string `json:"type"`
}
type Filter struct {
DayStart string `json:"dayStart"`
DayEnd string `json:"dayEnd"`
TimePeriods interface{} `json:"timePeriods"`
Lines []string `json:"lines"`
}
type Task struct {
Date string
Request ChartOptions
}
type Work struct {
Name string
Data interface{}
}
请问我应该如何更好地编写正确的switch语句,如果缓存的结构体类型可能不同的话?为什么我的名称添加会出错?
英文:
I have a trouble in a part of code. I'm writing on revel framework(to be clear). This is a Worker go routine, and I want it to do several things:
-
switch the struct type of the stat variable, according to the
source, that would come. I made a switch, but before the all other
code would be correct, I don't really know if switch is written
properly. -
I get cache for the date, and put it in new Work item.
-
I send Work to channel
here is what I got by now:
func worker(in <-chan Task, out chan <- Work, wg *sync.WaitGroup) {
for t := range in {
for sourceName, charts := range t.Request.Charts {
var stat interface{}
switch sourceName {
case "noagg":
stat = stat.([]NoaggModel)
case "oracle":
stat = stat.([]OracleModel)
default:
panic("Invalid type for Work model!")
}
w := Work{Name:"", Data:""}
err := cache.Get(string(sourceName)+"_"+string(t.Date), &stat);
for chart := range charts{
w.Name = chart["name"]
if err == nil{
w.Data = countDataByName( stat, t.Request.Filters, string(chart["name"]))
}
out <- w
}
}
}
wg.Done() // this worker is now done; let the WaitGroup know.
}
But now I got error that invalid operation: chart["name"] (type int does not support indexing)
But I have structs :
type Chart struct {
Name string `json:"name"`
Type string `json:"type"`
}
type Filter struct {
DayStart string `json:"dayStart"`
DayEnd string `json:"dayEnd"`
TimePeriods interface{} `json:"timePeriods"`
Lines []string `json:"lines"`
}
type Task struct {
Date string
Request ChartOptins
}
type Work struct {
Name string
Data interface{}
}
How should I write in a better way the correct switch, if the type of struct for cache can be different, and why is my name adding is bad and call error?
答案1
得分: 8
在切片的for循环中缺少一个变量
for chart := range charts{
当在切片上进行迭代时,第一个变量是键,第二个变量是你想要的真实值。在这种情况下,你可以省略键(一个int),所以正确的指令应该是
for _, chart := range charts{
英文:
The for in the slice is missing a variable
for chart := range charts{
when iterating on a slice the first variable is the key and the second is the real value you want. In this case you can omit the key (an int) so the proper instruction should be
for _, chart := range charts{
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论