将继承的结构作为基础对象传回

我确定这是一个语法问题,我还没有用 Go 解决 -


我得到的错误——


不能在extractBucket的参数中使用*term(类型elastic.AggregationBucketKeyItem)作为类型elastic.Aggregations


产生错误的那一行是


"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),

                }

            }

        }

}


料青山看我应如是
浏览 126回答 2
2回答

拉风的咖菲猫

嵌入类型会使您“继承”该类型,这是一个常见的误解。即使AggregationBucketSignificantTerms 嵌入了Aggregations,它也不是编译器的一个。它只有一个 type 字段Aggregations,并在其顶层提供该类型的方法。感觉有点像继承,但可能不是您对 Java 子类之类的东西所习惯的。要解决它,您可以尝试"Value": extractBucket(parts[1:], *term.Aggregations),,但我不清楚这是否会解决您的问题。

慕侠2389804

好吧,错误是不言自明的:不能使用 (*term, variable name) (type elastic.AggregationBucketKeyItem <-- Variables current type) as (type elastic.Aggregations <-- Expected type) in argument to extractBucket不管你的*term价值产生者:&nbsp;for _, term := range terms.Buckets {不是函数的正确类型extractBucket(parts&nbsp;[]string,&nbsp;aggs&nbsp;elastic.Aggregations)采取一种&nbsp;elastic.Aggregations
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go