```go
package createandcontrol
import (
"reflect"
"testing"
)
type student struct {
Name string
Age int
}
func TestReflect(t *testing.T) {
var (
model *student
st reflect.Type
elem reflect.Value
)
//讲道理这里的一系列转换如何在内存中体现的我还是没整明白
st = reflect.TypeOf(model)
st = st.Elem()
elem = reflect.New(st) //这里创建的新指针但是是空指针
//println(elem) //我试图在这里取elem的地址发现取不出来
println(&elem)
model = elem.Interface().(*student) //转空接口加类型断言一气呵成
elem = elem.Elem() //可是这里elem是空指针呀
elem.FieldByName("Name").SetString("yufeng")
elem.FieldByName("Age").SetInt(18)
t.Log(model, st, model.Name, model.Age)
}
```
有疑问加站长微信联系(非本文作者)