July 2019
Intermediate to advanced
458 pages
12h 12m
English
A map type allows you to get the type of both value and key, using the Key and Elem methods:
func main() { maps := []interface{}{ make(map[string]struct{}), make(map[int]rune), make(map[float64][]byte), make(map[int32]chan bool), make(map[[2]string]interface{}), } for _, m := range maps { t := reflect.TypeOf(m) fmt.Printf("%s k:%-10s v:%-10s\n", m, t.Key(), t.Elem()) }}
A full example is available here: https://play.golang.org/p/j__1jtgy-56.
The values can be accessed in all the ways that a map can be accessed normally:
Let's see how it works in a practical example:
func main() { m := map[string]int64{ "a": 10, "b": 20, "c": 100, "d": 42, } v := reflect.ValueOf(m) ...Read now
Unlock full access