structgosizeof

Sizeof struct in Go


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 ?


Solution

  • 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.