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

时间:2017-03-15 06:01:51

标签: go

我是Golang&的新手。尝试将批量上传脚本构建到Elasticsearch。写了一个示例代码来做这件事,但得到一个简单的错误"missing type in composite literal"

我谷歌这个。有一些参考文章,但我真的无法无花果。我失踪了。

我的main.go文件 -

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)
    }
}

需要一些帮助。

1 个答案:

答案 0 :(得分:2)

您应named type / ProductSpec / DisplaySpec使用FilterSpec

试试这个:

 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"}}}}

请参阅named/unamed type {{1}}。