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

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

使用反射檢查分配給 interface{} 變量的函數簽名

使用反射檢查分配給 interface{} 變量的函數簽名

Go
Qyouu 2021-07-29 21:19:02
我正在嘗試構建一個 通用的 咖喱 看起來像這樣的功能:package curryimport (    "fmt"    "reflect")// Functiontype fn interface{}// Function parametertype pr interface{}// It return the curried functionfunc It(f fn, p ...pr) (fn, error) {    // examine the concret type of the function f    if reflect.ValueOf(f).Kind() == reflect.Func {    // Get the slice of input and output parameters type     } else {        return nil, fmt.Errorf("%s", "takes a function as a first parameter")    }    // _, _ = f, p    return nil, nil}是否可以[]reflect.Type從函數中提取輸入和輸出參數類型的切片f?
查看完整描述

2 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

Go 1.5 將添加一個可以在這里提供幫助的功能。(回顧 1996 年,由Dave ( )提交 e1c1fa2)okdave


// FuncOf returns the function type with the given argument and result types.

// For example if k represents int and e represents string,

// FuncOf([]Type{k}, []Type{e}, false) represents func(int) string.

//

// The variadic argument controls whether the function is variadic. FuncOf

// panics if the in[len(in)-1] does not represent a slice and variadic is

// true.

func FuncOf(in, out []Type, variadic bool) Type

測試用例包括以下有趣的代碼:


v := MakeFunc(FuncOf([]Type{TypeOf(K(""))}, []Type{TypeOf(V(0))}, false), fn)

outs := v.Call([]Value{ValueOf(K("gopher"))})


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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