2 回答

守著一只汪
TA貢獻1872條經驗 獲得超4個贊
下面是一個工作示例:
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"},
},
}
}

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
當然,更好的解決方法是顯式聲明,但初始實現也不是太糟糕。struct{id string}
在你的聲明上,你有你的內聯類型在哪里。因此,您唯一缺少的是內聯結構的額外大括號和重新聲明: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 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消