2016-08-16 114 views
-1

執行此Go程序時出現以下錯誤。不知道我錯過了什麼。「複合文字中缺少類型」錯誤

.\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]) 
} 

請幫助。

回答

2

使用

LTI{Leads: []LI{leadIdInput}} 

fmt.Printf("rCR is '%+v' \n", rCR.Input.Leads[0]) 

嘗試在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' \n", rCR.Input.Leads[0]) 
} 

輸出:

Hello, playground 
rCR is '{Id:55213}' 
+0

@skm我希望這有助於。 – 2016-08-16 03:17:56