golang,int类型不支持索引操作。

huangapple go评论83阅读模式
英文:

golang, type int does not support indexing

问题

我在代码的一部分遇到了问题。我正在使用revel框架进行编写(为了清楚起见)。这是一个Worker go例程,我希望它能完成几件事情:

  1. 根据来源,切换stat变量的结构类型。我使用了一个switch语句,但在所有其他代码正确之前,我不确定switch语句是否编写正确。

  2. 我获取日期的缓存,并将其放入新的Work项目中。

  3. 我将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:

  1. 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.

  2. I get cache for the date, and put it in new Work item.

  3. I send Work to channel

here is what I got by now:

func worker(in &lt;-chan Task, out chan &lt;- Work, wg *sync.WaitGroup) {

	for t := range in {
		for sourceName, charts := range t.Request.Charts {

			var stat interface{}

			switch sourceName {
			case &quot;noagg&quot;:
				 stat = stat.([]NoaggModel)
			case &quot;oracle&quot;:
				stat = stat.([]OracleModel)
			default:
				panic(&quot;Invalid type for Work model!&quot;)
			}

			w := Work{Name:&quot;&quot;, Data:&quot;&quot;}

			err := cache.Get(string(sourceName)+&quot;_&quot;+string(t.Date), &amp;stat);

			for chart := range charts{
				w.Name = chart[&quot;name&quot;]

				if err == nil{
					w.Data = countDataByName( stat, t.Request.Filters, string(chart[&quot;name&quot;]))
				}
				out &lt;- w
			}
		}
	}
	wg.Done() // this worker is now done; let the WaitGroup know.
}

But now I got error that invalid operation: chart[&quot;name&quot;] (type int does not support indexing)

But I have structs :

type Chart struct {
	Name string           `json:&quot;name&quot;`
	Type string           `json:&quot;type&quot;`
}

type Filter struct {
	DayStart string `json:&quot;dayStart&quot;`
	DayEnd string `json:&quot;dayEnd&quot;`
	TimePeriods interface{} `json:&quot;timePeriods&quot;`
	Lines []string `json:&quot;lines&quot;`
}

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{

huangapple
  • 本文由 发表于 2016年2月11日 17:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35335416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定