Golang reflect.MakeMap() function example

package reflect

Golang reflect.MakeMap() function usage example

 package main

 import (
  "fmt"
  "reflect"
 )

 func main() {
  var str map[int]string // must be a map type for reflect.MakeMap() to work

  var strValue reflect.Value = reflect.ValueOf(&str)

  indirectStr := reflect.Indirect(strValue)

  valueMap := reflect.MakeMap(indirectStr.Type()) // otherwise, will panice

  fmt.Printf("valueMap type is [%v] .", valueMap.Kind())

 }

Output :

valueMap type is [map] .

Reference :

http://golang.org/pkg/reflect/#MakeMap

Advertisement