Go中结构的大小 [英] Sizeof struct in Go

查看:127
本文介绍了Go中结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看看Go,看起来很有希望。
我试图弄清楚如何得到一个去结构的大小,对于
的例子来说就像

 类型Coord3d struct {
X,Y,Z int64
}

当然我知道它是24字节,但我想通过编程的方式了解它。



你有什么想法如何做到这一点?

$

/ *描述一个inotify事件的结构。

/ *描述一个inotify事件的结构。 * /
type INotifyInfo struct {
Wd int32 //观察描述符
掩码uint32 //观察掩码
Cookie uint32 // Cookie同步两个事件
Len uint32 /名称
}

的长度(包括NULs)func doSomething(){
var info INotifyInfo
const infoSize = unsafe.Sizeof(info)
...
}

注意: 。不安全的.Sizeof在示例Coord3d结构上返回24。请参阅下面的评论。


I'm having a look at Go, which looks quite promising. I am trying to figure out how to get the size of a go struct, for example something like

type Coord3d struct {
    X, Y, Z int64
}

Of course I know that it's 24 bytes, but I'd like to know it programmatically..

Do you have any ideas how to do this ?

解决方案

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)
    ...
}

NOTE: The OP is mistaken. The unsafe.Sizeof does return 24 on the example Coord3d struct. See comment below.

这篇关于Go中结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆