beetree
beetree

Reputation: 941

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 {
    Base
    c string
}

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

f(Something{
    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!

Upvotes: 33

Views: 39235

Answers (4)

Jinsong Li
Jinsong Li

Reputation: 7388

Read the example from Go by Example: Struct Embedding, the trick part is that we need to explicitly initialize the embedded struct, but we can directly access the embedded fields.

type base struct {
    num int
}
type container struct {
    base
    str string
}
// initialize the embedding explicitly
co := container{
    base: base{
       num: 1,
    },
    str: "some name",
}
// however, the embedded field can be accessed directly
fmt.Printf("co={num: %v, str: %v}\n", co.num, co.str)
// this also works
fmt.Println("also num:", co.base.num)

Upvotes: 2

Leo Correa
Leo Correa

Reputation: 19789

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

Upvotes: 42

evanmcdonnal
evanmcdonnal

Reputation: 48086

You have to actually instantiate the embedded struct as well. Just so you know this isn't inheritance technically, no such feature exists in Go. It's called embedding. It just hoists fields and methods from the embedded type to the embeddors scope. So anyway, the composite literal instantiation you're trying to do would look like this;

f(Something{
    Base: Base{a: "a", b: "b"},
    c:    "c",
})

Upvotes: 5

Grzegorz Żur
Grzegorz Żur

Reputation: 49181

You need to explicitly create Base field like that

f(Something{
    Base: Base{a: "letter a"},
    c:    "letter c",
})

Go has no inheritance, it is just composition.

Upvotes: 11

Related Questions