Golang错误 – 组合字面量中缺少类型

huangapple go评论84阅读模式
英文:

Golang error - missing type in composite literal

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是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() {
	// 创建一个上下文
	ctx := context.Background()

	client, err := elastic.NewClient()
	if err != nil {
		fmt.Println("%v", err)
	}

	// 批量上传代码
	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: 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"`
			}{DisplaySpec: []struct {
				SpecID string `json:"spec_id"`
				Sdv    string `json:"sdv"`
				Snv    string `json:"snv"`
			}{{SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, {SpecID: "104", Sdv: "GSM", Snv: "0.0000"}}, FilterSpec: []struct {
				SpecID string `json:"spec_id"`
				Sdv    string `json:"sdv"`
				Snv    string `json:"snv"`
			}{{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)
	}
}

需要帮助

<details>
<summary>英文:</summary>

I&#39;m new to Golang &amp; trying to build bulk upload script to Elasticsearch. Wrote one sample code to do the thing but getting one simple error ```&quot;missing type in composite literal&quot;```. 

I google about this. Got some reference post but I really can&#39;t able to fig. out what I&#39;m missing.

**My main.go file** -

	package main

	import (
		&quot;fmt&quot;
		&quot;golang.org/x/net/context&quot;
		&quot;gopkg.in/olivere/elastic.v5&quot;
		&quot;strconv&quot;
	)

	type Product struct {
		ProductDisplayname string `json:&quot;product_displayname&quot;`
		ProductPrice       string `json:&quot;product_price&quot;`
		Popularity         string `json:&quot;popularity&quot;`
		Barcode            string `json:&quot;barcode&quot;`
		ExclusiveFlag      string `json:&quot;exclusive_flag&quot;`
		ProductID          string `json:&quot;product_id&quot;`
		ProductName        string `json:&quot;product_name&quot;`
		BrandName          string `json:&quot;brand_name&quot;`
		BrandID            string `json:&quot;brand_id&quot;`
		ProductSpec        struct {
			DisplaySpec []struct {
				SpecID string `json:&quot;spec_id&quot;`
				Sdv    string `json:&quot;sdv&quot;`
				Snv    string `json:&quot;snv&quot;`
			} `json:&quot;display_spec&quot;`
			FilterSpec []struct {
				SpecID string `json:&quot;spec_id&quot;`
				Sdv    string `json:&quot;sdv&quot;`
				Snv    string `json:&quot;snv&quot;`
			} `json:&quot;filter_spec&quot;`
		} `json:&quot;product_spec&quot;`
	}

	func main() {
		// Create a context
		ctx := context.Background()

		client, err := elastic.NewClient()
		if err != nil {
			fmt.Println(&quot;%v&quot;, err)
		}

		// Bulk upload code
		n := 0
		for i := 0; i &lt; 1000; i++ {
			bulkRequest := client.Bulk()
			for j := 0; j &lt; 10000; j++ {
				n++
				productData := Product{ProductDisplayname: &quot;LG Stylus 2 Plus K535D (16 GB, Brown)&quot;, ProductPrice: &quot;24000.00&quot;, Popularity: &quot;0.00&quot;, Barcode: &quot;&quot;, ExclusiveFlag: &quot;0&quot;, ProductID: &quot;17698276&quot;, ProductName: &quot;Stylus 2 Plus K535D (Brown)&quot;, BrandName: &quot;LG&quot;, BrandID: &quot;1&quot;, ProductSpec: {DisplaySpec: {SpecID: &quot;103&quot;, Sdv: &quot;24000&quot;, Snv: &quot;24000.0000&quot;}, {SpecID: &quot;104&quot;, Sdv: &quot;GSM&quot;, Snv: &quot;0.0000&quot;}, FilterSpec: {SpecID: &quot;103&quot;, Sdv: &quot;24000&quot;, Snv: &quot;24000.0000&quot;}, {SpecID: &quot;105&quot;, Sdv: &quot;Touch Screen&quot;, Snv: &quot;0.0000&quot;}}}
				req := elastic.NewBulkIndexRequest().Index(&quot;shopfront&quot;).Type(&quot;products&quot;).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.

</details>


# 答案1
**得分**: 5

你应该为`ProductSpec`/`DisplaySpec`/`FilterSpec`使用`named type`

试试这样写

```go
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的详细信息。

英文:

you should use named type for ProductSpec/DisplaySpec/FilterSpec.

try this:

 type DisplaySpecType struct {
SpecID string `json:&quot;spec_id&quot;`
Sdv    string `json:&quot;sdv&quot;`
Snv    string `json:&quot;snv&quot;`
}
type FilterSpecType struct {
SpecID string `json:&quot;spec_id&quot;`
Sdv    string `json:&quot;sdv&quot;`
Snv    string `json:&quot;snv&quot;`
}
type ProductSpecType struct {
DisplaySpec []DisplaySpecType `json:&quot;display_spec&quot;`
FilterSpec  []FilterSpecType  `json:&quot;filter_spec&quot;`
}
type Product struct {
ProductDisplayname string          `json:&quot;product_displayname&quot;`
ProductPrice       string          `json:&quot;product_price&quot;`
Popularity         string          `json:&quot;popularity&quot;`
Barcode            string          `json:&quot;barcode&quot;`
ExclusiveFlag      string          `json:&quot;exclusive_flag&quot;`
ProductID          string          `json:&quot;product_id&quot;`
ProductName        string          `json:&quot;product_name&quot;`
BrandName          string          `json:&quot;brand_name&quot;`
BrandID            string          `json:&quot;brand_id&quot;`
ProductSpec        ProductSpecType `json:&quot;product_spec&quot;`
}
var productData = Product{ProductDisplayname: &quot;LG Stylus 2 Plus K535D (16 GB, Brown)&quot;, ProductPrice: &quot;24000.00&quot;, Popularity: &quot;0.00&quot;, Barcode: &quot;&quot;, ExclusiveFlag: &quot;0&quot;, ProductID: &quot;17698276&quot;, ProductName: &quot;Stylus 2 Plus K535D (Brown)&quot;, BrandName: &quot;LG&quot;, BrandID: &quot;1&quot;, ProductSpec: ProductSpecType{DisplaySpec: []DisplaySpecType{DisplaySpecType{SpecID: &quot;103&quot;, Sdv: &quot;24000&quot;, Snv: &quot;24000.0000&quot;}, DisplaySpecType{SpecID: &quot;104&quot;, Sdv: &quot;GSM&quot;, Snv: &quot;0.0000&quot;}}, FilterSpec: []FilterSpecType{FilterSpecType{SpecID: &quot;103&quot;, Sdv: &quot;24000&quot;, Snv: &quot;24000.0000&quot;}, FilterSpecType{SpecID: &quot;105&quot;, Sdv: &quot;Touch Screen&quot;, Snv: &quot;0.0000&quot;}}}}

see here for named/unamed type.

huangapple
  • 本文由 发表于 2017年3月15日 14:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/42802136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定