“复合文字中缺少类型”错误[重复]
问题描述:
This question already has an answer here:
- Missing type in composite literal 1 answer
I am getting the following error, while executing this Go program. not sure what I am missing.
.\m.go:28: missing type in composite literal
.\m.go:28: too few values in struct initializer
package main
import (
"fmt"
)
type LI struct {
Id int `json:"id"`
}
type TP struct {
Name string `json:"name"`
Value string `json:"value"`
}
type LTI struct {
Leads []LI `json:"leads"`
Tokens []TP `json:"tokens,omitempty"`
}
type RCR struct {
Input LTI `json:"input"`
}
func main() {
fmt.Println("Hello, playground")
leadIdInput := LI{Id: 55213}
leadTokensInput := LTI{{[]LI{leadIdInput}, nil}}
rCR := RCR{Input: leadTokensInput}
fmt.Println("rCR is '%+v'", rCR.Input.Leads[0])
}
Please help.
</div>
此问题已经存在 在这里有答案: p>
-
复合文字中的缺少类型
1个答案
span>
li>
ul>
div>
在执行此Go程序时出现以下错误。 p>
。\ m.go:28:复合文字中的类型丢失 。\ m.go:28:结构初始化程序中的值太少 code> pre>
答
Use
LTI{Leads: []LI{leadIdInput}}
and
fmt.Printf("rCR is '%+v'
", rCR.Input.Leads[0])
Try it on The Go Playground:
package main
import (
"fmt"
)
type LI struct {
Id int `json:"id"`
}
type TP struct {
Name string `json:"name"`
Value string `json:"value"`
}
type LTI struct {
Leads []LI `json:"leads"`
Tokens []TP `json:"tokens,omitempty"`
}
type RCR struct {
Input LTI `json:"input"`
}
func main() {
fmt.Println("Hello, playground")
leadIdInput := LI{Id: 55213}
leadTokensInput := LTI{Leads: []LI{leadIdInput}}
rCR := RCR{Input: leadTokensInput}
fmt.Printf("rCR is '%+v'
", rCR.Input.Leads[0])
}
output:
Hello, playground
rCR is '{Id:55213}'