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

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

Golang error - missing type in composite literal

问题

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

我是Golang的新手,尝试构建一个批量上传脚本到Elasticsearch的脚本。我写了一段示例代码来完成这个任务,但是遇到了一个简单的错误"missing type in composite literal"。

我在谷歌上搜索了一下,找到了一些参考帖子,但是我真的无法弄清楚我缺少了什么。

我的main.go文件如下:

  1. package main
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "gopkg.in/olivere/elastic.v5"
  6. "strconv"
  7. )
  8. type Product struct {
  9. ProductDisplayname string `json:"product_displayname"`
  10. ProductPrice string `json:"product_price"`
  11. Popularity string `json:"popularity"`
  12. Barcode string `json:"barcode"`
  13. ExclusiveFlag string `json:"exclusive_flag"`
  14. ProductID string `json:"product_id"`
  15. ProductName string `json:"product_name"`
  16. BrandName string `json:"brand_name"`
  17. BrandID string `json:"brand_id"`
  18. ProductSpec struct {
  19. DisplaySpec []struct {
  20. SpecID string `json:"spec_id"`
  21. Sdv string `json:"sdv"`
  22. Snv string `json:"snv"`
  23. } `json:"display_spec"`
  24. FilterSpec []struct {
  25. SpecID string `json:"spec_id"`
  26. Sdv string `json:"sdv"`
  27. Snv string `json:"snv"`
  28. } `json:"filter_spec"`
  29. } `json:"product_spec"`
  30. }
  31. func main() {
  32. // 创建一个上下文
  33. ctx := context.Background()
  34. client, err := elastic.NewClient()
  35. if err != nil {
  36. fmt.Println("%v", err)
  37. }
  38. // 批量上传代码
  39. n := 0
  40. for i := 0; i < 1000; i++ {
  41. bulkRequest := client.Bulk()
  42. for j := 0; j < 10000; j++ {
  43. n++
  44. 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 {
  45. DisplaySpec []struct {
  46. SpecID string `json:"spec_id"`
  47. Sdv string `json:"sdv"`
  48. Snv string `json:"snv"`
  49. } `json:"display_spec"`
  50. FilterSpec []struct {
  51. SpecID string `json:"spec_id"`
  52. Sdv string `json:"sdv"`
  53. Snv string `json:"snv"`
  54. } `json:"filter_spec"`
  55. }{DisplaySpec: []struct {
  56. SpecID string `json:"spec_id"`
  57. Sdv string `json:"sdv"`
  58. Snv string `json:"snv"`
  59. }{{SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, {SpecID: "104", Sdv: "GSM", Snv: "0.0000"}}, FilterSpec: []struct {
  60. SpecID string `json:"spec_id"`
  61. Sdv string `json:"sdv"`
  62. Snv string `json:"snv"`
  63. }{{SpecID: "103", Sdv: "24000", Snv: "24000.0000"}, {SpecID: "105", Sdv: "Touch Screen", Snv: "0.0000"}}}}
  64. req := elastic.NewBulkIndexRequest().Index("shopfront").Type("products").Id(strconv.Itoa(n)).Doc(productData)
  65. bulkRequest = bulkRequest.Add(req)
  66. }
  67. bulkResponse, err := bulkRequest.Do(ctx)
  68. if err != nil {
  69. fmt.Println(err)
  70. }
  71. if bulkResponse != nil {
  72. fmt.Println(bulkResponse)
  73. }
  74. fmt.Println(i)
  75. }
  76. }
  77. 需要帮助
  78. <details>
  79. <summary>英文:</summary>
  80. 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;```.
  81. I google about this. Got some reference post but I really can&#39;t able to fig. out what I&#39;m missing.
  82. **My main.go file** -
  83. package main
  84. import (
  85. &quot;fmt&quot;
  86. &quot;golang.org/x/net/context&quot;
  87. &quot;gopkg.in/olivere/elastic.v5&quot;
  88. &quot;strconv&quot;
  89. )
  90. type Product struct {
  91. ProductDisplayname string `json:&quot;product_displayname&quot;`
  92. ProductPrice string `json:&quot;product_price&quot;`
  93. Popularity string `json:&quot;popularity&quot;`
  94. Barcode string `json:&quot;barcode&quot;`
  95. ExclusiveFlag string `json:&quot;exclusive_flag&quot;`
  96. ProductID string `json:&quot;product_id&quot;`
  97. ProductName string `json:&quot;product_name&quot;`
  98. BrandName string `json:&quot;brand_name&quot;`
  99. BrandID string `json:&quot;brand_id&quot;`
  100. ProductSpec struct {
  101. DisplaySpec []struct {
  102. SpecID string `json:&quot;spec_id&quot;`
  103. Sdv string `json:&quot;sdv&quot;`
  104. Snv string `json:&quot;snv&quot;`
  105. } `json:&quot;display_spec&quot;`
  106. FilterSpec []struct {
  107. SpecID string `json:&quot;spec_id&quot;`
  108. Sdv string `json:&quot;sdv&quot;`
  109. Snv string `json:&quot;snv&quot;`
  110. } `json:&quot;filter_spec&quot;`
  111. } `json:&quot;product_spec&quot;`
  112. }
  113. func main() {
  114. // Create a context
  115. ctx := context.Background()
  116. client, err := elastic.NewClient()
  117. if err != nil {
  118. fmt.Println(&quot;%v&quot;, err)
  119. }
  120. // Bulk upload code
  121. n := 0
  122. for i := 0; i &lt; 1000; i++ {
  123. bulkRequest := client.Bulk()
  124. for j := 0; j &lt; 10000; j++ {
  125. n++
  126. 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;}}}
  127. req := elastic.NewBulkIndexRequest().Index(&quot;shopfront&quot;).Type(&quot;products&quot;).Id(strconv.Itoa(n)).Doc(productData)
  128. bulkRequest = bulkRequest.Add(req)
  129. }
  130. bulkResponse, err := bulkRequest.Do(ctx)
  131. if err != nil {
  132. fmt.Println(err)
  133. }
  134. if bulkResponse != nil {
  135. fmt.Println(bulkResponse)
  136. }
  137. fmt.Println(i)
  138. }
  139. }
  140. Need some help.
  141. </details>
  142. # 答案1
  143. **得分**: 5
  144. 你应该为`ProductSpec`/`DisplaySpec`/`FilterSpec`使用`named type`
  145. 试试这样写
  146. ```go
  147. type DisplaySpecType struct {
  148. SpecID string `json:"spec_id"`
  149. Sdv string `json:"sdv"`
  150. Snv string `json:"snv"`
  151. }
  152. type FilterSpecType struct {
  153. SpecID string `json:"spec_id"`
  154. Sdv string `json:"sdv"`
  155. Snv string `json:"snv"`
  156. }
  157. type ProductSpecType struct {
  158. DisplaySpec []DisplaySpecType `json:"display_spec"`
  159. FilterSpec []FilterSpecType `json:"filter_spec"`
  160. }
  161. type Product struct {
  162. ProductDisplayname string `json:"product_displayname"`
  163. ProductPrice string `json:"product_price"`
  164. Popularity string `json:"popularity"`
  165. Barcode string `json:"barcode"`
  166. ExclusiveFlag string `json:"exclusive_flag"`
  167. ProductID string `json:"product_id"`
  168. ProductName string `json:"product_name"`
  169. BrandName string `json:"brand_name"`
  170. BrandID string `json:"brand_id"`
  171. ProductSpec ProductSpecType `json:"product_spec"`
  172. }
  173. var productData = Product{
  174. ProductDisplayname: "LG Stylus 2 Plus K535D (16 GB, Brown)",
  175. ProductPrice: "24000.00",
  176. Popularity: "0.00",
  177. Barcode: "",
  178. ExclusiveFlag: "0",
  179. ProductID: "17698276",
  180. ProductName: "Stylus 2 Plus K535D (Brown)",
  181. BrandName: "LG",
  182. BrandID: "1",
  183. ProductSpec: ProductSpecType{
  184. DisplaySpec: []DisplaySpecType{
  185. DisplaySpecType{
  186. SpecID: "103",
  187. Sdv: "24000",
  188. Snv: "24000.0000",
  189. },
  190. DisplaySpecType{
  191. SpecID: "104",
  192. Sdv: "GSM",
  193. Snv: "0.0000",
  194. },
  195. },
  196. FilterSpec: []FilterSpecType{
  197. FilterSpecType{
  198. SpecID: "103",
  199. Sdv: "24000",
  200. Snv: "24000.0000",
  201. },
  202. FilterSpecType{
  203. SpecID: "105",
  204. Sdv: "Touch Screen",
  205. Snv: "0.0000",
  206. },
  207. },
  208. },
  209. }

这里查看named/unamed type的详细信息。

英文:

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

try this:

  1. type DisplaySpecType struct {
  2. SpecID string `json:&quot;spec_id&quot;`
  3. Sdv string `json:&quot;sdv&quot;`
  4. Snv string `json:&quot;snv&quot;`
  5. }
  6. type FilterSpecType struct {
  7. SpecID string `json:&quot;spec_id&quot;`
  8. Sdv string `json:&quot;sdv&quot;`
  9. Snv string `json:&quot;snv&quot;`
  10. }
  11. type ProductSpecType struct {
  12. DisplaySpec []DisplaySpecType `json:&quot;display_spec&quot;`
  13. FilterSpec []FilterSpecType `json:&quot;filter_spec&quot;`
  14. }
  15. type Product struct {
  16. ProductDisplayname string `json:&quot;product_displayname&quot;`
  17. ProductPrice string `json:&quot;product_price&quot;`
  18. Popularity string `json:&quot;popularity&quot;`
  19. Barcode string `json:&quot;barcode&quot;`
  20. ExclusiveFlag string `json:&quot;exclusive_flag&quot;`
  21. ProductID string `json:&quot;product_id&quot;`
  22. ProductName string `json:&quot;product_name&quot;`
  23. BrandName string `json:&quot;brand_name&quot;`
  24. BrandID string `json:&quot;brand_id&quot;`
  25. ProductSpec ProductSpecType `json:&quot;product_spec&quot;`
  26. }
  27. 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:

确定