亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

你如何在go中獲得指向類型函數的函數指針?

你如何在go中獲得指向類型函數的函數指針?

Go
RISEBY 2021-06-05 08:33:45
以下代碼獲取指向該函數的指針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


查看完整回答
反對 回復 2021-06-07
  • 2 回答
  • 0 關注
  • 316 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號