我有一些旧的C代码,有点大量使用压缩结构.我正在研究使用Go作为此代码的包装器,但是很难找到传递甚至为这些结构编写定义的方法.
例:
import "unsafe";
type AlignTest struct {
c byte;
y int16;
z int16;
q int32;
}
func main() {
vr := new(AlignTest);
fmt.Println(unsafe.Sizeof(*vr), "\n");
}
Run Code Online (Sandbox Code Playgroud)
使用打包/未对齐的结构返回12而不是1 + 2 + 2 + 4 = 9.
我知道我可以创建一个字节数组并手动进行解析,但这看起来非常脆弱且容易出错......