英文:
Why do struct declarations sometimes contain blank fields?
问题
从golang规范中可以看到:
// 一个有6个字段的结构体。
struct {
    x, y int
    u float32
    _ float32  // 填充字段
    A *[]int
    F func()
}
- 在
struct内部使用空白_字段的实际场景有哪些?(提供一些代码片段会很有帮助) 
英文:
From the golang spec
// A struct with 6 fields.
struct {
	x, y int
	u float32
	_ float32  // padding
	A *[]int
	F func()
}
- Are there any practical scenarios of using the blank 
_fields inside astruct? (some code snippets would be appreciated) 
答案1
得分: 9
填充(padding)就是它的字面意思:一些填充用来对齐后续字段,以满足你的需求,例如匹配C结构的布局。它无法被访问(至少不能通过unsafe包访问)。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论