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

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

數組數組中特定位置的類型?

數組數組中特定位置的類型?

Go
翻過高山走不出你 2022-04-20 17:40:26
我是 Go 的新手,我正在嘗試用這個一般方面構建一個函數:mapOfResults = ThingDoer([  ["One",    int,    -1,    true],  ["Flying", string, "",    true],  ["Banana", bool,   false, true]])但我什至無法弄清楚它的簽名(簽名甚至是 Go 中它的專有名詞嗎?它所有參數的定義等)。我說的是這個結構:func ThingDoer(config ThisIsWhatICannotFigure) map[string]Results {    // the body of my function}如何定義此類參數的類型?
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

試試這個:


 type ConfigItem struct {

    Name string

    Value interface{}

    SomethingElse bool

 }


mapOfResults = ThingDoer([]ConfigItem{

  {"One",    -1,    true},

  {"Flying", "",    true},

  {"Banana", false, true},

})

ThingDoer 可以使用類型開關來確定值類型:


func ThingDoer(config []ConfigItem) map[foo]bar {

    for _, item := range config {

      switch v := item.Value.(type) {

      case int:

        // v is int

      case bool:

        // v is bool

      case string:

        // v is string

      }

    }

 }


查看完整回答
反對 回復 2022-04-20
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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