37
A
回答
44
为了做到这一点,您需要reflect
。
package main
import (
"fmt"
"reflect"
)
func main() {
// one way is to have a value of the type you want already
a := 1
// reflect.New works kind of like the built-in function new
// We'll get a reflected pointer to a new int value
intPtr := reflect.New(reflect.TypeOf(a))
// Just to prove it
b := intPtr.Elem().Interface().(int)
// Prints 0
fmt.Println(b)
// We can also use reflect.New without having a value of the type
var nilInt *int
intType := reflect.TypeOf(nilInt).Elem()
intPtr2 := reflect.New(intType)
// Same as above
c := intPtr2.Elem().Interface().(int)
// Prints 0 again
fmt.Println(c)
}
你可以用结构类型来代替int来做同样的事情。或者其他任何东西,真的。当涉及到map和slice类型时,一定要知道new和make之间的区别。
15
您可以使用reflect.Zero()
这将返回结构类型的零值的表示形式。 (类似,如果你没有var foo StructType
)这是reflect.New()
因为后者将动态分配的结构,给你一个指针,类似的不同来new(StructType)
18
由于reflect.New
不会自动在结构领域中引用类型,你可以使用类似下面的递归初始化这些字段类型(注意在这个例子中,递归结构定义):
package main
import (
"fmt"
"reflect"
)
type Config struct {
Name string
Meta struct {
Desc string
Properties map[string]string
Users []string
}
}
func initializeStruct(t reflect.Type, v reflect.Value) {
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
ft := t.Field(i)
switch ft.Type.Kind() {
case reflect.Map:
f.Set(reflect.MakeMap(ft.Type))
case reflect.Slice:
f.Set(reflect.MakeSlice(ft.Type, 0, 0))
case reflect.Chan:
f.Set(reflect.MakeChan(ft.Type, 0))
case reflect.Struct:
initializeStruct(ft.Type, f)
case reflect.Ptr:
fv := reflect.New(ft.Type.Elem())
initializeStruct(ft.Type.Elem(), fv.Elem())
f.Set(fv)
default:
}
}
}
func main() {
t := reflect.TypeOf(Config{})
v := reflect.New(t)
initializeStruct(t, v.Elem())
c := v.Interface().(*Config)
c.Meta.Properties["color"] = "red" // map was already made!
c.Meta.Users = append(c.Meta.Users, "srid") // so was the slice.
fmt.Println(v.Interface())
}
0
这里有一个基本的例子像埃文·肖给了,但有一个结构:
package main
import (
"fmt"
"reflect"
)
func main() {
type Product struct {
Name string
Price string
}
var product Product
productType := reflect.TypeOf(product) // this type of this variable is reflect.Type
productPointer := reflect.New(productType) // this type of this variable is reflect.Value.
productValue := productPointer.Elem() // this type of this variable is reflect.Value.
productInterface := productValue.Interface() // this type of this variable is interface{}
product2 := productInterface.(Product) // this type of this variable is product
product2.Name = "Toothbrush"
product2.Price = "2.50"
fmt.Println(product2.Name)
fmt.Println(product2.Price)
}
每newacct的反应,使用Reflect.zero这将是:
var product Product
productType := reflect.TypeOf(product) // this type of this variable is reflect.Type
productValue := reflect.Zero(productType) // this type of this variable is reflect.Value
productInterface := productValue.Interface() // this type of this variable is interface{}
product2 := productInterface.(Product) // the type of this variable is Product
This is a great article在去反思的基础知识。
相关问题
- 1. GO运行时类型结构的新实例
- 2. 如何在运行时使用c#创建类的新实例?
- 3. 如何创建类的实例在运行时创建
- 4. 如何在运行时在java中创建新添加的类的实例
- 5. GAE,如何在创建新模型实例时运行模型的功能?
- 6. 如何在C中创建结构的新实例C
- 7. 如何在QML中创建Q_GADGET结构的新实例?
- 8. 如何创建“动态(在运行时决定)”类的实例?
- 9. 在从父类创建实例时进行类型转换
- 10. 如何从其基类的实例创建一个新对象?
- 11. 在运行时C#结构实例化
- 12. 如何让基于类型的结构图构建实例
- 13. 如何从(静态)类中的类创建新的类实例?
- 14. 创建运行时确定类型实例的最佳方法
- 15. 如何从现有实例创建相同类型的新实例?
- 16. 如何为Go创建JSON结构
- 17. 如何创建一个数据结构,其中包含在运行时定义类型的属性
- 18. Rails - 在创建新用户时创建其他类的新实例
- 19. 在运行时在Unity中创建类实例
- 20. 如何instanciate在运行时创建一个新类型?
- 21. 在运行时创建类的实例,同时初始化
- 22. 在运行时创建实例变量?
- 23. 创建实例在运行时
- 24. 如何在创建类的新实例时转换为类型vb.net
- 25. 当参数化类型通过层次结构时,在Java中创建泛型类型的实例?
- 26. 在Go中创建一个包含未导出子结构的实例
- 27. 在Android中创建新类的实例
- 28. 如何从GO结构中获取嵌入式类型?
- 29. 如何在haskell中创建类的类型实例?
- 30. Visual Studio - 计算在运行时创建的类的实例
发布了此答案的一个版本,对于 –