4 ms·
Can you give a code example in Go?
by p0nce 12y ago
Can you give a code example in Go?
- cjslep 12y agoQuick example on the playground, if this is what you were asking about: http://play.golang.org/p/zShJKp0t3n http://play.golang.org/p/zShJKp0t3n
- eco 12y agoSo subtyping automatically happens for all members of a struct? I'm not sure I like that but I guess I'd have to see how well it works in practice.
- 4ad 12y agoNo, it only happens if you embed the type, a.i. the member is anonymous. http://golang.org/doc/effective_go.html#embedding http://golang.org/doc/effective_go.html#embedding http://golang.org/ref/spec#Struct_types http://golang.org/ref/spec#Struct_types Note that there's also interface embedding which is a different thing.
- eco 12y agoAh, that makes a lot more sense. Thank you for explaining.
- 4ad 12y agohttp://play.golang.org/p/Vl9XzK3hOS http://play.golang.org/p/Vl9XzK3hOS
- p0nce 12y agoInteresting but this is not the same feature. Go implements what seems like multiple inheritance, D simply dispatch calls to a member, which can be a pointer to T instead of T. So you can implement custom pointer types.
- 4ad 12y agoI have provided a better example: http://play.golang.org/p/yYg8okmN6a http://play.golang.org/p/yYg8okmN6a Yes, it's a different feature, but is not multiple inheritance either (although I understand why, at a first glance, you'd think it would seems like that). It's just composition. In this new example, look how you can do b.myInt, even though b is now a bar, not a foo. In fact this happened in the old example too, but it was invisible. This is how the Lock function accessed the mutex.