“复合文字中缺少类型”错误[重复]

“复合文字中缺少类型”错误[重复]

问题描述:

This question already has an answer here:

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

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{{[]LI{leadIdInput}, nil}}
    rCR := RCR{Input: leadTokensInput}
    fmt.Println("rCR is '%+v'", rCR.Input.Leads[0])
}

Please help.

</div>

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