Skip to content

Instantly share code, notes, and snippets.

@adityagodbole
Created October 26, 2016 09:11
Show Gist options
  • Save adityagodbole/5ed0f48b9a1c72aff544100fff59dfb0 to your computer and use it in GitHub Desktop.
Save adityagodbole/5ed0f48b9a1c72aff544100fff59dfb0 to your computer and use it in GitHub Desktop.
package main
import "log"
type EyeReq interface {
setI(int)
getI() int
}
type EyeTrait struct {
EyeReq
}
func (f EyeTrait) eye() {
f.setI(10)
}
/////////////////////////
type JayReq interface {
setJ(int)
getJ() int
}
type JayTrait struct {
JayReq
}
func (f JayTrait) jay() {
f.setJ(20)
}
///////////////////////////
type Cl struct {
i, j int
}
func (cl *Cl) getI() int {
return cl.i
}
func (cl *Cl) setI(i int) {
cl.i = i
}
func (cl *Cl) getJ() int {
return cl.j
}
func (cl *Cl) setJ(j int) {
cl.j = j
}
func (cl *Cl) WithEyeTrait() EyeTrait {
return EyeTrait{cl}
}
func (cl *Cl) WithJayTrait() JayTrait {
return JayTrait{cl}
}
///////////////////////////////////////
type EyeJayTrait struct {
EyeTrait
JayTrait
}
func (cl *Cl) WithEyeJayTrait() EyeJayTrait {
return EyeJayTrait{
EyeTrait: cl.WithEyeTrait(),
JayTrait: cl.WithJayTrait(),
}
}
////////////////////////////
///////////////////////////////////////////
func main() {
cl := Cl{}
// clFoo := cl.WithEyeTrait()
// clFoo.eye()
// clBar := cl.WithJayTrait()
// clBar.jay()
clFooBar := cl.WithEyeJayTrait()
clFooBar.eye()
clFooBar.jay()
log.Printf("cl.getI() = %+v\n", cl.getI())
log.Printf("cl.getJ() = %+v\n", cl.getJ())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment