Go结构:多个嵌套字段,但只能指定一个字段



我想知道如何在Go中实现继承。阅读后,我明白了我必须考虑结构嵌入。由于我是一名Kubernetes开发人员,我会跳到Kubernete源代码中,并开始阅读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文件的源代码中以编程方式编写的?因为我在源代码中寻找它,在那里它可以通过编程进行验证,但我没有看到。

求你了,你能帮我理解吗?

在语言规范中没有限制structs字段值的事情。然而,我有一个很好的感觉,因为所有字段都是指针,所以卷源的方法会检查所有字段,并希望只有一个字段是非零的。

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

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")
}
}
}

最新更新