我正在研究 Go,它看起来很有希望。 我试图弄清楚如何获得 go 结构的大小,因为 例如
type Coord3d struct {
X, Y, Z int64
}
我当然知道它是 24 字节,但我想以编程方式了解它..
你有什么想法吗?
最佳答案
import unsafe "unsafe"
/* Structure describing an inotify event. */
type INotifyInfo struct {
Wd int32 // Watch descriptor
Mask uint32 // Watch mask
Cookie uint32 // Cookie to synchronize two events
Len uint32 // Length (including NULs) of name
}
func doSomething() {
var info INotifyInfo
const infoSize = unsafe.Sizeof(info)
...
}
注意: OP 是错误的。 unsafe.Sizeof 在示例 Coord3d 结构上确实返回 24。请参阅下面的评论。
关于struct - Go中的结构体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2113751/