16

In the following code I show what I think is the difference between embedding (where methods get promoted) and composition (where methods are not promoted) in golang.

Why would you ever want to use composition in golang?

type obj1Inherited struct {
    obj2
}

type obj1Composed struct {
    someobj obj2
}

type obj2 struct {
}

func (o obj2) printTest() {
    fmt.Println("obj2")
}

func main() {
    o := obj1Inherited{}
    o.printTest() //fine - printTest is promoted

    obj1Composed := obj1Composed{}
    obj1Composed.someobj.printTest() //fine because I'm using the composed obj
    obj1Composed.printTest() //not fine - printTest is NOT promoted
1
  • 7
    Can someone please explain why this was down voted? I've asked this same question 3 different ways now on SO and every time it's not answered and there's no helpful comments.
    – Charlie
    Apr 19, 2016 at 12:26

4 Answers 4

17

It is worth going over the section on Embedding in Effective Go.

A common example is having a struct/map with a Mutex.

type SafeStruct struct {
    SomeField string 
    *sync.Mutex
}

It is much easier to type

safe := SafeStruct{SomeField: "init value"}

safe.Lock()
defer safe.Unlock()
safe.SomeField = "new value"

than having to either write appropriate wrapper functions (which are repetitive) or have the stutter of

safe.mutex.Unlock()

when the only thing you would ever do with the mutex field is access the methods (Lock() and Unlock() in this case)

This becomes even more helpful when you are trying to use multiple functions on the embedded field (that implemement an interface like io.ReadWriter).

10
  • I simplified the question. It has nothing to do with inheritance.
    – Charlie
    Apr 19, 2016 at 12:27
  • 1
    Also for what's it worth - I've seen far "stupider" questions on SO for more established languages that are still at least answered and not heavily downvoted. This has made a strong impression on me that the go community doesn't want to help newcomers.
    – Charlie
    Apr 19, 2016 at 12:53
  • @Charlie, I'm sorry you feel that way. I didn't think your question was "stupid" or downvote-worthy. The SO Go community seems different than on the Golang Forum or Slack Channel or Go-Nuts mailing list. A question about why/style might get a better response there. Hopefully my answer helped you out.
    – matt.s
    Apr 19, 2016 at 13:25
  • Thanks for pointing out the mutex case - that makes sense but seem like an edge case. As for the something like io.ReadWriter I thought that best practice would be "decorate" in which case you would want to promote methods so you could call something like o.save() which would then be implemented using something like io.Write(). Any thoughts on that? javacodegeeks.com/2014/09/…
    – Charlie
    Apr 19, 2016 at 15:16
  • 1
    @Charlie embedding is a helpful wrapper of composition. Composition is including the functionality/fields of smaller pieces in a larger vs inheritance, which is getting the functionality/fields by a relation to another class/object.
    – matt.s
    Apr 20, 2016 at 13:24
0

One more point I want to cover for first case in your example. If obj1Inherited and obj2 have a same name method then that method call (from obj1Inherited instance) will always execute obj1Inherited method.

To call obj2 method you can use the other approach of not promoting

0

I'll try to answer original question - sometimes people use "composition" instead of embedding to hide functionality of embedded struct. Not a great use case - but people prefer it sometimes.

type Obj1composed struct {
  notExportedObj1 obj1
}

func NewObj1Composed(someParam Params) Obj1composed {
...
}

func (o Obj1Composed) Print() {
  // Some heavy calculations here. Make Dozens of API calls
  // print some data
}
0

In Golang, there are 3 types of embed

  • struct embed in struct
  • interface embed in interface
  • interface embed in struct

In detail you can refer to this post: https://eli.thegreenplace.net/2020/embedding-in-go-part-1-structs-in-structs/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.