2016-02-29 55 views
-2

我敢肯定,这是一个语法的问题是我还没有一起去弄清楚 -传递一种遗传结构早在基础对象

我得到的错误 -

不能使用*术语(类型elastic.AggregationBucketKeyItem)作为参数类型elastic.Aggregations到extractBucket

正在生成错误的行是

"Value": extractBucket(parts[1:], *term), 

相关的代码,用于上下文

// from https://github.com/olivere/elastic/blob/v3.0.22/search_aggs.go 

type Aggregations map[string]*json.RawMessage 

type AggregationBucketSignificantTerms struct { 
    Aggregations 

    DocCount int64        //`json:"doc_count"` 
    Buckets []*AggregationBucketSignificantTerm //`json:"buckets"` 
    Meta  map[string]interface{}    // `json:"meta,omitempty"` 
} 

// my code 

func extractBucket(parts []string, aggs elastic.Aggregations) interface{} { 
    // bunch of code removed 

      terms, found := aggs.Terms(part) 
      for _, term := range terms.Buckets { 
      if len(parts) == 0 { 
       retval[(term.Key).(string)] = map[string]interface{}{ 
        "Count": term.DocCount, 
       } 
      } else { 
       retval[(term.Key).(string)] = map[string]interface{}{ 
        "Count": term.DocCount, 
        "Value": extractBucket(parts[1:], *term), 
       } 
      } 
     } 
} 

回答

2

它是一种常见的误解是嵌入型让你“继承“那种类型。即使AggregationBucketSignificantTerms嵌入Aggregations,它不是编译器。它只有一个类型为Aggregations的字段,并且它在顶层提供了该类型的方法。这感觉有点像继承,但可能不是你习惯的东西,如Java子类。

要解决它,你可以尝试"Value": extractBucket(parts[1:], *term.Aggregations),,但我不清楚这是否能解决你的问题。

+0

我在发布后大约5分钟就弄明白了,但是来自C++世界(Java有类似的模式),它不像我习惯的那种行为。 – koblas

2

嘛错误是非常自我解释:

不能使用(*来看,变量名)(类型elastic.AggregationBucketKeyItem < - 变量电流型)为(类型elastic.Aggregations < - 在需要参数类型)extractBucket

无论你*term

通过生成:for _, term := range terms.Buckets {

不是为功能

extractBucket(parts []string, aggs elastic.Aggregations) 

正确的类型需要一个类型的elastic.Aggregations