亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用數組初始化結構

使用數組初始化結構

Go
夢里花落0921 2022-08-15 15:57:25
感興趣的結構如下所示type rect struct {width, height float64testArray []struct{    id    string  }}我正在嘗試初始化結構,如下所示r := rect{    width: 10,    height: 10,    testArray: []struct{        id: "wwwww",    },     {        id: "wwwww",    },}然而,它給我一個錯誤說語法錯誤: 意外 :, 預期類型
查看完整描述

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"},

      },

   }

}


查看完整回答
反對 回復 2022-08-15
?
慕俠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" },

     },

}


查看完整回答
反對 回復 2022-08-15
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號