This is not thoroughly tested for a wide range of values, but it works for a few simple cases. Feel free to expand it further for your own needs. It's also not guaranteed to be anything like the best way to do this, and error checking is left as an exercise for the reader.
func parseArguments(args ...interface{}) map[string]interface{} {
if args == nil {
return nil
}
if x,ok := args[0].(map[string]interface{}); ok {
return x
}
x := map[string]interface{}{}
for i := 0; i < len(args); i += 2 {
x[ args[i].(string) ] = args[i+1]
}
return x
}
func DoSomethingCool(x ...interface{}) {
args := parseArguments(x);
// args is now a map of type map[string]interface{}
}
You can now call DoSomethingCool()
in any of the following ways:
// No arguments
DoSomethingCool()
// These two are equivalent, and result in
// args = map[string]interface{}{
// "foo": "bar",
// "baz": "qux",
// }
DoSomethingCool(map[string]string{"foo": "bar", "baz": "qux"})
DoSomethingCool("foo","bar","baz","qux")
// Or you can even mix types for the keys, thanks to interface{}
// These two are also equivalents and result in
// args = map[string]interface{}{
// "foo": 20,
// "bar": false,
// }
DoSomethingCool(map[string]interface{}{"foo": 20, "bar": false})
DoSomethingCool("foo",20,"bar",false)
If you don't have a need to mix value types, you could just as well use map[string]string
I imagine.