A different approach is to return T
not map[T][]T
play
:
type T interface{}
func B() T {
result := map[string][]string{
"test": {"test", "test"},
}
return T(result)
}
func A() map[string][]string {
res := B()
if v, ok := res.(map[string][]string); ok {
return v
}
return nil
}
func main() {
fmt.Println("Hello, playground", A())
}
// Edit, converter function : http://play.golang.org/p/cW_PNTqauV
func makeMap() map[T][]T {
return map[T][]T{
"test": {"test", "test"},
"stuff": {"stuff", 1212, "stuff"},
1: {10, 20},
}
}
func convertMap(in map[T][]T) (out map[string][]string) {
out = make(map[string][]string, len(in))
for k, _ := range in {
if ks, ok := k.(string); ok {
v := in[k] // this way we won't use a copy in the for loop
out[ks] = make([]string, 0, len(v))
for i := range v {
if vs, ok := v[i].(string); ok {
out[ks] = append(out[ks], vs)
} else {
fmt.Printf("Error: %v (%T) is not a string.
", v[i], v[i])
}
}
} else {
fmt.Printf("Error: %v (%T) is not a string.
", k, k)
}
}
return
}
func main() {
fmt.Println(convertMap(makeMap()))
}