【问题标题】:What is the string after the name and type in a struct in Golang? [duplicate]Golang 结构中名称和类型后面的字符串是什么? [复制]
【发布时间】:2019-08-27 21:31:25
【问题描述】:

我在看https://godoc.org/k8s.io/api/core/v1#Secret

type Secret struct {
    metav1.TypeMeta `json:",inline"`
    // Standard object's metadata.
    // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
    // +optional
    metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

    // Data contains the secret data. Each key must consist of alphanumeric
    // characters, '-', '_' or '.'. The serialized form of the secret data is a
    // base64 encoded string, representing the arbitrary (possibly non-string)
    // data value here. Described in https://tools.ietf.org/html/rfc4648#section-4
    // +optional
    Data map[string][]byte `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"`

    // stringData allows specifying non-binary secret data in string form.
    // It is provided as a write-only convenience method.
    // All keys and values are merged into the data field on write, overwriting any existing values.
    // It is never output when reading from the API.
    // +k8s:conversion-gen=false
    // +optional
    StringData map[string]string `json:"stringData,omitempty" protobuf:"bytes,4,rep,name=stringData"`

    // Used to facilitate programmatic handling of secret data.
    // +optional
    Type SecretType `json:"type,omitempty" protobuf:"bytes,3,opt,name=type,casttype=SecretType"`
}

采取 例如Data map[string][]byte `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"` ,我知道Data 是名称,map[string][]byte 是类型,那么接下来的第三件事是什么?它的作用是什么?什么时候需要包含这第三件事?

【问题讨论】:

    标签: go struct kubernetes-secrets


    【解决方案1】:

    json:"data,omitempty" protobuf:"bytes,2,rep,name=data" 被称为结构标签。关于该主题的一些有用链接是:

    • 官方语言规范here
    • 一些非常知名的here
    • 一个如何创建自定义的教程here

    标签是结构定义的一部分,允许您告诉结构如何存储数据、创建映射、进行验证等。事实上,您会在处理数据的 Go 包中看到很多标签。

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 2013-03-08
      • 2020-09-19
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2021-11-05
      相关资源
      最近更新 更多