2 回答
TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊
下面是一個(gè)工作示例:
type test struct {
id string
}
type rect struct {
height float64
width float64
testArray []test
}
func main() {
r := rect{
height: 10,
width: 10,
testArray: []test{
{id: "April"},
{id: "March"},
},
}
}
TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
當(dāng)然,更好的解決方法是顯式聲明,但初始實(shí)現(xiàn)也不是太糟糕。struct{id string}
在你的聲明上,你有你的內(nèi)聯(lián)類型在哪里。因此,您唯一缺少的是內(nèi)聯(lián)結(jié)構(gòu)的額外大括號(hào)和重新聲明:testArray []struct{id string}struct { id string }
r := rect{
width: 10,
height: 10,
testArray: []struct{ id string} { // re declare the inline struct type
{ id: "April" }, // provide values
{ id: "March" },
},
}
- 2 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報(bào)
