Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. I've been using Go for a while now and I find it to be a very pleasant programming language.
  2. However, there's one thing I cant really get a grip on. Something that I used to do a lot in the past, that I cant figure out how to do with Go.
  3. I'm talking about inheritance.
  4.  
  5. Go doesn't have inheritance, and the reasons for that are very clear to me. However, now there is no inheritance, I can't figure out how to solve this problem.
  6.  
  7. I have objects: which should "inherit" each other:
  8. Object (should have properties: Name string, Description string
  9. Item (is an Object, should have a properties: Weight int, ID int(ID is a unique integer that's being used for item lookup in a map))
  10. Note (is an Item, should have a property: Text string)
  11.  
  12. Also there's an Object called Inventory, which contains a slice with Items.
  13.  
  14. What is Go's way to "extend" structs?
  15. I can solve this by making Object and Item interfaces, and giving them methods called Name(), Description() and Weight(). Then make Note implement those interfaces by giving it the Name() Description(), Weight() and ID() methods. But this gives me the problem that I have to copy paste exactly the same ID() code to every implementation of Item (which will later be a lot more then just Note)..
  16. To me, this seems like a very ugly solution, and I assume there should be a way to make Item have it's own (implemented) methods while Note is still implementing Item (and can use the for Item created methods).
  17.  
  18. So, what's Go's way to do this??
  19. Thanks in advance!
  20. --GeertJohan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement