Go中的sizeof结构

约格·豪布里奇斯(JörgHaubrichs)

我正在看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。请参阅下面的评论。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章