有沒有辦法將[分配給變量]的函數(shù)的名稱Printf?從游樂場出發(fā):func hello(name string) string { return fmt.Sprintf("Hello, %s", name)}func main() { testFunc := hello fmt.Println(testFunc("DoubleNNs")) fmt.Printf("%v", testFunc)}/*Output:./prog.go:14:2: Printf format %v arg testFunc is a func value, not calledGo vet exited.Hello, DoubleNNs0x499200Program exited.*/我找到了指導(dǎo)如何打印被調(diào)用函數(shù)名稱的答案,但不要獲取任意函數(shù)的字符串表示形式。
1 回答

收到一只叮咚
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
可以使用反射
包來獲取函數(shù),然后將其傳遞給運(yùn)行時(shí)。用于
獲取有關(guān)函數(shù)的信息。uintptr
runtime.FuncForPC(reflect.ValueOf(testFunc).Pointer()).Name()
https://play.golang.org/p/T3pmjd6ds1i
- 1 回答
- 0 關(guān)注
- 83 瀏覽
添加回答
舉報(bào)
0/150
提交
取消