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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)