mercredi 27 juin 2018

Using golang reflect, can I do type declaration dynamically?

package main

import (
    "fmt"
    "reflect"
)

type Config struct {
    Name string
    Age  int
}

func main() {
    a := make(map[string]Config)
    a["m"] = Config{"mingukang", 29}
    t := reflect.TypeOf(a["m"])
    v := reflect.New(t)
    c := v.Interface().(*Config)
    fmt.Println(c)

}

In c := v.Interface().(*Config), I want to replace *Config to variable contains Type value like a:= *Config and declare it like, c := v.Interface().(a)

Is it possible in golang?





Aucun commentaire:

Enregistrer un commentaire