Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lesismal
Last active February 21, 2022 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesismal/e2203edd06a17fa5043bbfdbf6cbbaf7 to your computer and use it in GitHub Desktop.
Save lesismal/e2203edd06a17fa5043bbfdbf6cbbaf7 to your computer and use it in GitHub Desktop.
use go like class
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)
}
@lesismal
Copy link
Author

output:

go run class.go
----- AB -----
foo a
foo b

----- CA -----
foo a
foo ca-b

----- CB -----
foo cb-a
foo b

----- CAB -----
foo cab-a
foo cab-b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment