Last active
February 21, 2022 10:58
-
-
Save lesismal/e2203edd06a17fa5043bbfdbf6cbbaf7 to your computer and use it in GitHub Desktop.
use go like class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type IAB interface { | |
FooA() | |
FooB() | |
} | |
type A struct{} | |
func (a *A) FooA() { | |
println("foo a") | |
} | |
type B struct{} | |
func (a *A) FooB() { | |
println("foo b") | |
} | |
type AB struct { | |
A | |
B | |
} | |
type CA struct { | |
A | |
} | |
func (ca *CA) FooB() { | |
println("foo ca-b") | |
} | |
type CB struct { | |
A | |
B | |
} | |
func (cb *CB) FooA() { | |
println("foo cb-a") | |
} | |
type CAB struct { | |
A | |
B | |
} | |
func (cab *CAB) FooA() { | |
println("foo cab-a") | |
} | |
func (cab *CAB) FooB() { | |
println("foo cab-b") | |
} | |
func FooAB(ab IAB) { | |
ab.FooA() | |
ab.FooB() | |
} | |
func main() { | |
println("----- AB -----") | |
ab := &AB{} | |
FooAB(ab) | |
println("\n----- CA -----") | |
ca := &CA{} | |
FooAB(ca) | |
println("\n----- CB -----") | |
cb := &CB{} | |
FooAB(cb) | |
println("\n----- CAB -----") | |
cab := &CAB{} | |
FooAB(cab) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: