1. 程式人生 > >golang 動態new interface{}

golang 動態new interface{}

package main

import (
    "fmt"
    "reflect"
)

type A struct{
    AA int
    AB string
}

func test(a interface{}){
    t := reflect.TypeOf(a)
    v:= reflect.New(t).Elem()
    w:= reflect.New(t).Elem().Interface().(A)
    fmt.Println(reflect.TypeOf(a), reflect.TypeOf(v), reflect.TypeOf(w))
}

func main(){
    ret
:= A{ AA:1, AB:"test1", } test(ret) }