为什么结构声明有时包含空白字段? [英] Why do struct declarations sometimes contain blank fields?

查看:42
本文介绍了为什么结构声明有时包含空白字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 golang规范

// A struct with 6 fields.
struct {
    x, y int
    u float32
    _ float32  // padding
    A *[]int
    F func()
}

  • 是否有在 struct 中使用空白 _ 字段的实际方案?(一些代码片段将不胜感激)
    • Are there any practical scenarios of using the blank _ fields inside a struct? (some code snippets would be appreciated)
    • 推荐答案

      该填充正是所谓的填充:一些填充可以使以下字段与您的需求保持一致,例如匹配C结构的布局.无法访问它(至少在没有软件包不安全的情况下).

      The padding is exactly what it is called: Some padding to align the following field to your needs, e.g. to match the layout of a C struct. It cannot be accessed (at least not without package unsafe).

      这篇关于为什么结构声明有时包含空白字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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