I want to have something like named, ordered parameters in Go, and came to
the following solution:
package main
import "fmt"
type param struct {
name, value string
}
func main() {
show([]param{{"x", "1"}, {"y", "b"}, {"z", "abc"}})
}
func show(params []param) {
for _, item := range params {
fmt.Println(item.name + ": " + item.value)
}
}
http://play.golang.org/p/MddW-Rpuyg
Simulating named parameters with show([]param{{"x", "1"}, {"y", "b"}, {"z",
"abc"}}) looks ok, even though I'd prefer show(x="1", y="b", z="abc") like
in Python. I tried a map, which has a litte bit more concise syntax, but
lacks a defined order. Any better idea?
In real life the values are not always strings and have to be converted
therefore. I tried interface{}, but that didn't help because the format
string depends on the actual type. Then I tried fmt.Stringer interface, but
that ist not implement by string, int and other fundamental types. Is there
a way to handle this?
--