dongzhi8487 2017-03-15 06:01
浏览 269
已采纳

Golang错误-复合文字中缺少类型[重复]

This question already has an answer here:

I'm new to Golang & trying to build bulk upload script to Elasticsearch. Wrote one sample code to do the thing but getting one simple error "missing type in composite literal".

I google about this. Got some reference post but I really can't able to fig. out what I'm missing.

My main.go file -

package main

import (
    "fmt"
    "golang.org/x/net/context"
    "gopkg.in/olivere/elastic.v5"
    "strconv"
)

type Product struct {
    ProductDisplayname string `json:"product_displayname"`
    ProductPrice       string `json:"product_price"`
    Popularity         string `json:"popularity"`
    Barcode            string `json:"barcode"`
    ExclusiveFlag      string `json:"exclusive_flag"`
    ProductID          string `json:"product_id"`
    ProductName        string `json:"product_name"`
    BrandName          string `json:"brand_name"`
    BrandID            string `json:"brand_id"`
    ProductSpec        struct {
        DisplaySpec []struct {
            SpecID string `json:"spec_id"`
            Sdv    string `json:"sdv"`
            Snv    string `json:"snv"`
        } `json:"display_spec"`
        FilterSpec []struct {
            SpecID string `json:"spec_id"`
            Sdv    string `json:"sdv"`
            Snv    string `json:"snv"`
        } `json:"filter_spec"`
    } `json:"product_spec"`
}

func main() {
    // Create a context
    ctx := context.Background()

    client, err := elastic.NewClient()
    if err != nil {
        fmt.Println("%v", err)
    }

    // Bulk upload code
    n := 0
    for i := 0; i < 1000; i++ {
        bulkRequest := client.Bulk()
        for j := 0; j < 10000; j++ {
            n++
            productData := Product{ProductDisplayname: "LG Stylus 2 Plus K535D (16 GB, Brown)", ProductPrice: "24000.00", Popularity: "0.00", Barcode: "", ExclusiveFlag: "0", ProductID: "17698276", ProductName: "Stylus 2 Plus K535D (Brown)", BrandName: "LG", BrandID: "1", ProductSpec: {DisplaySpec: {SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, {SpecID: "104", Sdv: "GSM", Snv: "0.0000"}, FilterSpec: {SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, {SpecID: "105", Sdv: "Touch Screen", Snv: "0.0000"}}}
            req := elastic.NewBulkIndexRequest().Index("shopfront").Type("products").Id(strconv.Itoa(n)).Doc(productData)
            bulkRequest = bulkRequest.Add(req)
        }

        bulkResponse, err := bulkRequest.Do(ctx)
        if err != nil {
            fmt.Println(err)
        }
        if bulkResponse != nil {
            fmt.Println(bulkResponse)
        }
        fmt.Println(i)
    }
}

Need some help.

</div>
  • 写回答

1条回答 默认 最新

  • douliang1891 2017-03-15 06:29
    关注

    you should use named type for ProductSpec/DisplaySpec/FilterSpec.

    try this:

     type DisplaySpecType struct {
            SpecID string `json:"spec_id"`
            Sdv    string `json:"sdv"`
            Snv    string `json:"snv"`
    }
    
    type FilterSpecType struct {
            SpecID string `json:"spec_id"`
            Sdv    string `json:"sdv"`
            Snv    string `json:"snv"`
    }
    type ProductSpecType struct {
            DisplaySpec []DisplaySpecType `json:"display_spec"`
            FilterSpec  []FilterSpecType  `json:"filter_spec"`
    }
    
    type Product struct {
            ProductDisplayname string          `json:"product_displayname"`
            ProductPrice       string          `json:"product_price"`
            Popularity         string          `json:"popularity"`
            Barcode            string          `json:"barcode"`
            ExclusiveFlag      string          `json:"exclusive_flag"`
            ProductID          string          `json:"product_id"`
            ProductName        string          `json:"product_name"`
            BrandName          string          `json:"brand_name"`
            BrandID            string          `json:"brand_id"`
            ProductSpec        ProductSpecType `json:"product_spec"`
    }
    
    var productData = Product{ProductDisplayname: "LG Stylus 2 Plus K535D (16 GB, Brown)", ProductPrice: "24000.00", Popularity: "0.00", Barcode: "", ExclusiveFlag: "0", ProductID: "17698276", ProductName: "Stylus 2 Plus K535D (Brown)", BrandName: "LG", BrandID: "1", ProductSpec: ProductSpecType{DisplaySpec: []DisplaySpecType{DisplaySpecType{SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, DisplaySpecType{SpecID: "104", Sdv: "GSM", Snv: "0.0000"}}, FilterSpec: []FilterSpecType{FilterSpecType{SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, FilterSpecType{SpecID: "105", Sdv: "Touch Screen", Snv: "0.0000"}}}}
    

    see here for named/unamed type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?