2017-03-15 4 views
-1

私はGolang &にElasticsearchへの一括アップロードスクリプトを作成しようとしています。 1つの単純なエラー"missing type in composite literal"を取得することを1つのサンプルコードを書いた。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

ProductSpec/DisplaySpec/FilterSpecにはnamed typeを使用する必要があります。

はこれを試してみてください。

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ためhereを参照してください。

+0

これで動作します。助けてくれてありがとう。私はゴランにとって少し新しいです。私はちょうどcodechool.comからのコースを通過し、これに飛び乗った。だからこそ、これらの深刻な批判のすべてを認識していないのです。あなたは私にこのようなトリッキーなことを理解させるのに役立つゴランのリンクをいくつか紹介してくれますか? – mi6crazyheart

+0

https://golang.org/doc/で時間を費やすことは非常に価値があります。 '言語仕様'/'有効な実行/' FAQ'は不可欠です。 – zzn

+0

ありがとうございました。 – mi6crazyheart

関連する問題