简体   繁体   中英

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?

type Foo struct {
    data object
}

All Go types implement the empty interface interface{} .

type Foo struct {
   data interface{}
}

The empty interface is covered in A Tour of Go , The Laws of Reflection and the specification .

Starting in Go 1.18 you can use any<\/code> — alias of interface{}<\/code> as type of field or variable. It looks better than interface{}<\/code> .

type Foo struct {
   data any
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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