0

I am a golang newbie so pardon me if there is something obvious I am missing. I have the following structures:

type base interface {
    func1()
    func2()
    common_func()
}

type derived1 struct {
    base // anonymous meaning inheritence
    data DatumType
}

type derived2 struct {
    base // anonymous meaning inheritence
    data DatumType
}

Now I want to do the following:

  1. Keep 'data DatumType' with base in some way so that looking at the definition of base one can know what data is common to all structs.
    1. Implement common_func() in one place so that the derived structs don't need to do it.

I tried implementing the function with the interface but it fails compilation. I tried to create a struct and inherit from that but am not finding good ways to do that. Is there some clean way out ?

5
  • 1
    Having an anonymous interface member in your struct like this is probably not what you want. It will cause your struct to implement the interface, but probably not in the way you'd think. Any method of interface you don't implement on your struct will end up being delegated to this member. And if you've left it set to nil, you've turned what could have been a simple compile error into a runtime error. Commented Jun 13, 2015 at 10:03
  • Thanks for the note @JamesHenstridge. I thought that was the recommended way as per many online resources. Having a name makes it look ugly. Commented Jun 15, 2015 at 20:32
  • So why the -1 ? Please add a note. Commented Jun 15, 2015 at 20:32
  • @JamesHenstridge: the issue you mentioned should pass if the base has a default implementation of all methods, correct ? Commented Jun 15, 2015 at 21:39
  • An interface has no implementation. Think of it more like a Java interface (without the need to explicitly state you implement it), rather than a C++ class with a few abstract virtual methods. Commented Jun 16, 2015 at 8:04

2 Answers 2

3

Go does not have inheritance. Instead it offers the concept of embedding.

In this case you don't need/want to define base as an interface. Make it a struct and define the functions as methods. Then embedding base in your derived structs will give them those methods.

type base struct{
    data DatumType     
}
func (b base) func1(){

}

func (b base) func2(){

}

func (b base) common_func(){

}

type derived1 struct {
    base // anonymous meaning embedding
}

type derived2 struct {
    base // anonymous meaning embedding
}

Now you can do:

d := derived1{}
d.func1()
d.func2()
d.common_func()

And (as pointed out by David Budworth in the comment) you can access the fields of base by referring to it's type name like so:

d.base.data = something
Sign up to request clarification or add additional context in comments.

2 Comments

You don't have to name the embedded struct as you can access it by name of type (d.base.data in this case). Example: play.golang.org/p/x5BISwEWI8
Tks, didn't know that! It's actually mentioned in the Effective Go section I linked but I didn't get from that that this works for any field. Good to know. Edited my answer accordingly.
2

There is no inheritance in Go. Use composition:

type common struct {
    data DatumType
}

func (c *common) commonFunc() {
    // Do something with data.
}

type derived1 struct {
    common
    otherField1 int
}

// Implement other methods on derived1.

type derived2 struct {
    common
    otherField2 string
}

// Implement other methods on derived2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.