次のコードは、関数へのポインターを取得してhello
出力します。
package main
import "fmt"
type x struct {}
func (self *x) hello2(a int) {}
func hello(a int) {}
func main() {
f1 := hello
fmt.Printf("%+v\n", f1)
// f2 := hello2
// fmt.Printf("%+v\n", f2)
}
ただし、下部のセクションのコメントを解除すると、コンパイルエラーが発生し、次のようになります。
> ./junk.go:14: undefined: hello2
だから私は試しました:
i := &x{}
f2 := &i.hello2
fmt.Printf("%+v\n", f2)
...しかし、そのエラーは次のとおりです。
> ./junk.go:15: method i.hello2 is not an expression, must be called
さて、多分私は元のタイプを直接参照する必要があります:
f2 := x.hello2
fmt.Printf("%+v\n", f2)
いいえ:
> ./junk.go:14: invalid method expression x.hello2 (needs pointer receiver: (*x).hello2)
> ./junk.go:14: x.hello2 undefined (type x has no method hello2)
この種の作品:
i := &x{}
f2 := reflect.TypeOf(i).Method(0)
fmt.Printf("%+v\n", f2)
ただし、結果f2
はreflect.Method
関数ポインタではなく、です。:(
ここでの適切な構文は何ですか?