golang struct embedding

In golang it doesn't quite has inheritance, instead it uses embedding. For example, let's take a look at the following 

Here we are looking at embedding where C would be able to call Foo() directly or via c.A.Foo()


package main

type A struct{}

func (A) Foo() {}

type B struct{ A }
type C struct{ A }

func main() {

    c := C{}
    c.Foo()
    c.A.Foo()
}

Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type