以下代碼獲取指向該函數的指針hello并打印它:package mainimport "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,而不是函數指針。:(這里合適的語法是什么?
2 回答

largeQ
TA貢獻2039條經驗 獲得超8個贊
關于 Go 函數調用和閉包的相關閱讀:Russ Cox 的“Go 1.1 函數調用”(也詳細介紹了 Go1)。
https://docs.google.com/document/d/1bMwCey-gmqZVTpRax-ESeVuZGmjwbocYs1iHplK-cjo/pub
- 2 回答
- 0 關注
- 316 瀏覽
添加回答
舉報
0/150
提交
取消