2017-03-15 46 views
-1

缺少類型我是新來Golang &試圖建立批量上傳腳本Elasticsearch。寫了一個示例代碼來做這件事,但得到一個簡單的錯誤"missing type in composite literal"Golang錯誤 - 複合字面

我google一下這個。得到了一些參考文章,但我真的不能無花果。我失去了什麼。

我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

您應該使用named typeProductSpec/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"}}}} 

看到herenamed/unamed type

+0

它的工作現在。謝謝您的幫助。我對golang有點新鮮。我剛剛通過了codeschool.com的一門課程並跳入了這一章。這就是爲什麼沒有意識到所有這些深刻的關鍵。你能否介紹一些更多golang的鏈接,這些鏈接可以幫助我理解這些棘手的問題? – mi6crazyheart

+0

在https://golang.org/doc/上花費一些時間是非常值得的,「Language Specification」/'Effective Go' /'FAQ'是必不可少的。 – zzn

+0

明白了。再次感謝你。 – mi6crazyheart