英文:
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'm new to Golang & trying to build bulk upload script to Elasticsearch. Wrote one sample code to do the thing but getting one simple error ```"missing type in composite literal"```.
I google about this. Got some reference post but I really can't able to fig. out what I'm missing.
**My main.go file** -
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)
}
}
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:"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"}}}}
see here for named/unamed type
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论