2017-03-15 2 views
-1

Ich bin neu bei Golang & versuche Bulk Upload-Skript zu Elasticsearch zu erstellen. Schrieb einen Beispielcode, um die Sache zu tun, aber einen einfachen Fehler erhalten "missing type in composite literal".Golang-Fehler - fehlender Typ im Composite-Literal

Ich google darüber. Ich habe ein paar Referenzen, aber ich kann wirklich nicht fig. aus was ich vermisse.

Meine main.go Datei -

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

etwas Hilfe benötigen.

Antwort

1

sollten Sie named type für ProductSpec/DisplaySpec/FilterSpec verwenden.

versuchen Sie dies:

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

siehe here für named/unamed type.

+0

Es funktioniert jetzt. Danke für die Hilfe. Ich bin ein bisschen neu für Golang. Ich bin gerade durch einen Kurs von codeschool.com gegangen und bin in diesen hineingesprungen. Deshalb sind sich all diese tiefen Kritikalitäten nicht bewusst. Kannst du mir noch mehr Links für Golang empfehlen, die mir helfen, solche kniffligen Dinge zu verstehen? – mi6crazyheart

+0

Es lohnt sich sehr, etwas Zeit auf https://golang.org/doc/ zu verbringen, da 'Language Specification' /' Effective Go'/'FAQ' zwingend erforderlich sind. – zzn

+0

Verstanden. Danke nochmal. – mi6crazyheart

Verwandte Themen