I'm very much a newb in Go and I'm trying to build a function with this general aspect:
mapOfResults = ThingDoer([
["One", int, -1, true],
["Flying", string, "", true],
["Banana", bool, false, true]
])
But I cannot even figure its signature (is signature even the proper term for it in Go? the definition of all its params etc).
I'm talking about this construct:
func ThingDoer(config ThisIsWhatICannotFigure) map[string]Results {
// the body of my function
}
How do I define the types for such a parameter?
Try this:
type ConfigItem struct {
Name string
Value interface{}
SomethingElse bool
}
mapOfResults = ThingDoer([]ConfigItem{
{"One", -1, true},
{"Flying", "", true},
{"Banana", false, true},
})
The ThingDoer can use a type switch to determine the value types:
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
}
}
}