<p>I wanted to know how to implement inheritance in Go. After reading I understood that I must think about struct embedding. As I am a Kubernetes developer, I jump into the Kubernetes source code and start reading the PodSpec in which the <strong>volumes</strong> field is closer to what I am looking for.</p>
<p>When I thought I was starting to understand, something intrigued me.
By reading the snippet below, before the <code>type VolumeSource struct</code> you can read <strong>Only one of its members may be specified</strong></p>
<pre class="lang-golang prettyprint-override"><code>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:&quot;name&quot; protobuf:&quot;bytes,1,opt,name=name&quot;`
    // 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:&quot;,inline&quot; protobuf:&quot;bytes,2,opt,name=volumeSource&quot;`
}

// 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:&quot;hostPath,omitempty&quot; protobuf:&quot;bytes,1,opt,name=hostPath&quot;`
    // 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:&quot;emptyDir,omitempty&quot; protobuf:&quot;bytes,2,opt,name=emptyDir&quot;`
    // 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:&quot;gcePersistentDisk,omitempty&quot; protobuf:&quot;bytes,3,opt,name=gcePersistentDisk&quot;`
    // 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:&quot;awsElasticBlockStore,omitempty&quot; protobuf:&quot;bytes,4,opt,name=awsElasticBlockStore&quot;`
    // 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
    ....
</code></pre>
<p>How and where is this constraint satisfied?
Is it in the struct definition as it's written or programmatically in the source code in another .go file? Because I looked for it in the source code where it could be programmatically verified, but I didn't see.</p>
<p>Please, can you help me to understand?</p>

<p>there's no such thing to limit a structs field values in the <a href="https://golang.org/ref/spec#Struct_types" rel="nofollow noreferrer">language spec</a>. However, I have a good feeling that since all fields are pointers, a method of volume source checks for all fields and expects only one field to be non-nil.</p>
<p>Maybe something like this, I'm not sure of the usage of reflect though</p>
<pre><code>func (v VolumeSource) CheckFields() error {
    s := reflect.TypeOf(v)
    counter := 0
    for i:= s.NumField() - 1; i &gt;= 0 ; i-- {
        n := s.Field(i).Name()
        e := reflect.ValueOf(v).Field(i)
        if !e.IsValid(){
            continue
        }
        if e != nil {
            counter++
        }
        if counter &gt; 1{
            return errors.New(&quot;more than 1 field initialized&quot;)
        }

    }
}

</code></pre>
