0

Within a go package named kubesecretsPkg I declare the following two struct types:

type KubesecretsOpts struct {
    FullPathToRepo string
    FullPathToApp  string
}

type KubesecretsSetOpts struct {
    KubesecretsOpts
    Variable string
    Value    string
}

I am trying to initialise an outer (KubesecretsSetOpts) in another package as follows:

        kSetOpts := kubesecretsPkg.KubesecretsSetOpts{
            kubesecretsPkg.KubesecretsOpts: {
                FullPathToRepo: fullPathToRepo,
                FullPathToApp:  fullPathToApp,
            },
            Variable: variable,
            Value:    value,
        }

this fails as follows:

enter image description here

Why am I not able to directly initialise an embedded struct?

pkaramol
  • 16,451
  • 43
  • 149
  • 324

1 Answers1

1

The right syntax would be

   kSetOpts := kubesecretsPkg.KubesecretsSetOpts{
        KubesecretsOpts: kubesecretsPkg.KubesecretsOpts{
            FullPathToRepo: fullPathToRepo,
            FullPathToApp:  fullPathToApp,
        },
        Variable: variable,
        Value:    value,
    }

name of the field in the struct is "KubesecretsOpts" even in this scope however the type of the field is kubesecretsPkg.KubesecretsOpts. You are getting "invalid field name" error due to former fact in this scope.

Avnish
  • 1,241
  • 11
  • 19