This just makes the beauty of Rust so much more clear. Compatible C structs without going through this headache. It also points out another reason why Go is not a "systems" language.
I never understood why they didn't take the FFI route that Delphi, Pascal dialects, Modula-2, Modula-3, .NET, D, Ada, Rust and many other languages do, for describing bindings to other languages.
When I asked about why going cgo instead, the general reaction was "go away we know better", as usual.
> (By the way, I do mean 'Sysinfo' here, not 'Cpu_sysinfo'. Cgo is smart enough to take that sort of commonly seen prefix off of struct field names. I don't know what its algorithm is for doing this, but it's at least useful.)
Anyone know why cgo does this? This seems like an anti-feature to me – if I want fields with prefixes that are different from the C ones, at least make that a knob (and preferably one that isn't turned on by default).
$ go tool cgo -godefs test.go
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs test.go
package tmp
type BtrfsDiskKey struct {
Objectid uint64
Type uint8
Pad_cgo_0 [8]byte
}
After all the experiments I created my own helper[1], which helps map C structure(even packed) to Go structure using reflect package.
> It is still useless for me if you have struct with 'packed' attribute
It's somewhat sad that LuaJIT (effectively a one man project) contains a FFI interface with a hand-crafted lexer and tokenizer that supports the packed attribute correctly.