我是 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
}
}
}
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消
