Golang struct inheritance not working as intended?

Check out this sandbox

When declaring a struct that inherits from a different struct:

type Base struct {
    a string
    b string

type Something struct {
    c string

Then calling functions specifying values for the inherited values gives a compilation error:

    a: "letter a",
    c: "letter c",

The error message is: unknown Something field 'a' in struct literal.

This seems highly weird to me. Is this really the intended functionality?

Thanks for the help!


  • Golang doesn't provide the typical notion of inheritance. What you are accomplishing here is embedding.

    It does not give the outer struct the fields of the inner struct but instead allows the outer struct to access the fields of the inner struct.

    In order to create the outer struct Something you need to give its fields which include the inner struct Base

    In your case:

    Something{Base: Base{a: "letter a"}, c: "letter c"}