Skip to content

Instantly share code, notes, and snippets.

@suganoo suganoo/intf1.go
Created Feb 8, 2019

Embed
What would you like to do?
Golang polymorphisim
package main
import "fmt"
type MainInterface interface {
call()
}
type HogeFunc func()
func (h HogeFunc) call() {
h()
}
func main() {
var m MainInterface
var f = HogeFunc(func() {
fmt.Println("fffffffffffffffff")
})
m = f
m.call()
var a = HogeFunc(func() {
fmt.Println("aaaaaaaaaaaaaaaa")
f()
})
m = a
m.call()
}
package main
import "fmt"
type MainInterface interface {
call()
}
type Dog struct {
}
func (d *Dog) call() {
fmt.Println("doooooog")
}
type Cat struct {
}
func (c *Cat) call() {
fmt.Println("caaaaaaaat")
}
func main() {
var m MainInterface
d := Dog{}
m = &d
m.call()
c := Cat{}
m = &c
m.call()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.