Skip to content

Instantly share code, notes, and snippets.

@umamiMike
Last active March 19, 2020 07:59
Show Gist options
  • Save umamiMike/1770c06225376a7de346f190f93410bf to your computer and use it in GitHub Desktop.
Save umamiMike/1770c06225376a7de346f190f93410bf to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "reflect"
var fp = fmt.Println
func main() {
methodExpressionPlay2()
}
//The expression Foo.A yields a function equivalent to A but with an explicit receiver as its first argument; it has signature func(f Foo).
type Foo int
func (f Foo) A() {
fmt.Println("A")
}
func (f Foo) B() {
fmt.Println("B")
}
func (f Foo) C() {
fmt.Println("C")
}
var f Foo
func methodExpressionPlay2() {
bar := func(m func(f Foo)) {
m(f)
}
bar(Foo.A)
bar(Foo.B)
bar(Foo.C)
}
func methodExpressionPlay() {
var f Foo
bar := func(m func()) {
m()
}
bar(f.A)
bar(f.B)
bar(f.C)
}
func methoExpressionPlayReflect() {
var f Foo
bar := func(name string) {
reflect.ValueOf(f).MethodByName(name).Call(nil)
}
bar("A")
bar("B")
bar("C")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment