简体   繁体   English

"在 go 中声明泛型类型对象"

[英]declaring generic type object in go

Is there a generic type Object<\/code> in golang similar to other languages that can be assigned any kind of payload? golang 中是否有类似于其他语言的通用类型Object<\/code>可以分配任何类型的有效负载?

type Foo struct {
    data object
}

All Go types implement the empty interface interface{} . 所有Go类型都实现空接口interface{}

type Foo struct {
   data interface{}
}

The empty interface is covered in A Tour of Go , The Laws of Reflection and the specification . 空的界面包含在A Tour of GoThe Laws of Reflection规范中

Starting in Go 1.18 you can use any<\/code> — alias of interface{}<\/code> as type of field or variable.从 Go 1.18 开始,您可以使用any<\/code> — interface{}<\/code>的别名作为字段或变量的类型。 It looks better than interface{}<\/code> .它看起来比interface{}<\/code>更好。

type Foo struct {
   data any
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM