【问题标题】:Empty nested structure [duplicate]空嵌套结构[重复]
【发布时间】:2018-09-17 10:33:56
【问题描述】:

我在挖掘 AWS SDK 源代码时发现了以下类型声明:

type Condition struct {
    _ struct{} `type:"structure"`

    // the rest of code is removed
}

有人知道这种嵌入的目的/解释吗?

【问题讨论】:

  • 除其他用途外,还可以使用下划线的空结构字段(并且在一些地方被标准库使用)作为@987654321的零大小方法@.

标签: go


【解决方案1】:

它被用于代码生成。你可以把它想象成 java 把里面的所有东西都当作一个对象来对待。

使用它的例子是here

{{ $shapeList := ShapeListWithErrors $ }}
{{ range $_, $s := $shapeList }}
{{ if eq $s.Type "structure"}}{{ $s.GoCode }}{{ end }}
{{ end }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 2016-04-09
    • 1970-01-01
    • 2013-11-17
    • 2019-08-17
    • 1970-01-01
    • 2019-01-14
    • 2018-06-21
    相关资源
    最近更新 更多