I was looking for a way to convert a map to another map, without copying key by key. Both maps have the equivalent key type (as demonstrated below).
The code below seems to do the job, but I'm wondering what kind of pitfalls there might be if I use this?
package main
import (
"fmt"
"unsafe"
)
type A string
var (
x A = "x"
y A = "y"
)
func main() {
a := map[A]string{}
a[x] = "242342"
a[y] = "1234"
b := convert(a)
fmt.Println(a[x])
fmt.Println(b["x"])
fmt.Println(a[y])
fmt.Println(b["y"])
}
func convert(in map[A]string) map[string]string {
return *(*map[string]string)(unsafe.Pointer(&in))
}