登录
首页 >  Golang >  Go问答

Golang 结构体中冒号后面的字符串表示什么?

来源:stackoverflow

时间:2024-03-25 13:33:30 460浏览 收藏

在 Go 结构体中,冒号后面的字符串被称为结构标签。它们允许开发者指定结构体的元数据,例如如何存储数据、创建映射以及进行验证。结构标签通常用于与第三方库(如 JSON 编码器或验证器)进行交互。

问题内容

我正在查看 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"`
}

采取 数据映射[string][]byte `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"` 例如 ,我知道 data 是名称,map[string][]byte 是类型,接下来的第三件事是什么?它有什么作用以及什么时候有必要包含第三个东西?


解决方案


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

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

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

理论要掌握,实操不能落!以上关于《Golang 结构体中冒号后面的字符串表示什么?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>