登录
首页 >  Golang >  Go问答

Go struct:只能指定一个字段,即使有多个嵌套字段

来源:stackoverflow

时间:2024-02-15 08:54:22 165浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《Go struct:只能指定一个字段,即使有多个嵌套字段》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

我想知道如何在 go 中实现继承。读完后我明白我必须考虑结构嵌入。由于我是一名 kubernetes 开发人员,因此我跳入 kubernetes 源代码并开始阅读 podspec,其中 volumes 字段更接近我正在寻找的内容。

当我以为我开始理解时,有些东西引起了我的兴趣。 通过阅读下面的代码片段,在 type volumesource struct 之前,您可以阅读只能指定其成员之一

type Volume struct {
    // Volume's name.
    // Must be a DNS_LABEL and unique within the pod.
    // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
    Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
    // VolumeSource represents the location and type of the mounted volume.
    // If not specified, the Volume is implied to be an EmptyDir.
    // This implied behavior is deprecated and will be removed in a future version.
    VolumeSource `json:",inline" protobuf:"bytes,2,opt,name=volumeSource"`
}

// Represents the source of a volume to mount.
// Only one of its members may be specified.
type VolumeSource struct {
    // HostPath represents a pre-existing file or directory on the host
    // machine that is directly exposed to the container. This is generally
    // used for system agents or other privileged things that are allowed
    // to see the host machine. Most containers will NOT need this.
    // More info: https://kubernetes.io/docs/concepts/storage/volumes#hostpath
    // ---
    // TODO(jonesdl) We need to restrict who can use host directory mounts and who can/can not
    // mount host directories as read/write.
    // +optional
    HostPath *HostPathVolumeSource `json:"hostPath,omitempty" protobuf:"bytes,1,opt,name=hostPath"`
    // EmptyDir represents a temporary directory that shares a pod's lifetime.
    // More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir
    // +optional
    EmptyDir *EmptyDirVolumeSource `json:"emptyDir,omitempty" protobuf:"bytes,2,opt,name=emptyDir"`
    // GCEPersistentDisk represents a GCE Disk resource that is attached to a
    // kubelet's host machine and then exposed to the pod.
    // More info: https://kubernetes.io/docs/concepts/storage/volumes#gcepersistentdisk
    // +optional
    GCEPersistentDisk *GCEPersistentDiskVolumeSource `json:"gcePersistentDisk,omitempty" protobuf:"bytes,3,opt,name=gcePersistentDisk"`
    // AWSElasticBlockStore represents an AWS Disk resource that is attached to a
    // kubelet's host machine and then exposed to the pod.
    // More info: https://kubernetes.io/docs/concepts/storage/volumes#awselasticblockstore
    // +optional
    AWSElasticBlockStore *AWSElasticBlockStoreVolumeSource `json:"awsElasticBlockStore,omitempty" protobuf:"bytes,4,opt,name=awsElasticBlockStore"`
    // GitRepo represents a git repository at a particular revision.
    // DEPRECATED: GitRepo is deprecated. To provision a container with a git repo, mount an
    // EmptyDir into an InitContainer that clones the repo using git, then mount the EmptyDir
    // into the Pod's container.
    // +optional
    ....

如何以及在哪里满足此约束? 它是在结构定义中编写的还是以编程方式在另一个 .go 文件的源代码中?因为我在源代码中寻找过可以通过编程方式验证的地方,但没有看到。

请你帮我理解一下?


解决方案


language spec中没有这样的东西来限制结构体字段值。但是,我有一个很好的感觉,因为所有字段都是指针,所以卷源方法检查所有字段并期望只有一个字段为非零.

也许是这样的,但我不确定反射的用法

func (v VolumeSource) CheckFields() error {
    s := reflect.TypeOf(v)
    counter := 0
    for i:= s.NumField() - 1; i >= 0 ; i-- {
        n := s.Field(i).Name()
        e := reflect.ValueOf(v).Field(i)
        if !e.IsValid(){
            continue
        }
        if e != nil {
            counter++
        }
        if counter > 1{
            return errors.New("more than 1 field initialized")
        }

    }
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

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