struct - Go中的结构体大小

标签 struct go sizeof

我正在研究 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/

相关文章:

c - 从函数返回结构体的动态数组

C++ 神秘结构赋值与 int 截断

google-app-engine - 如何使用 Google App Engine Golang 的 SendGrid?

c++ - 有没有保证 sizeof(which) > 1 的简单类型?

C - sizeof(string) 结果小于应有的值

c++ - 避免在 C++ 中使用结构填充

Python & Ctypes : Passing a struct to a function as a pointer to get back data

go - 无法在 gotk3 应用程序中创建菜单栏

Golang 错误 "invalid memory address or nil pointer dereference",为什么会这样?

C sizeof char* 数组