cl/_testgo/reflectmkfn
This commit is contained in:
18
cl/_testgo/reflectmkfn/in.go
Normal file
18
cl/_testgo/reflectmkfn/in.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
typ := reflect.FuncOf([]reflect.Type{reflect.TypeOf(""), reflect.TypeOf(0)}, []reflect.Type{reflect.TypeOf("")}, false)
|
||||
fn := reflect.MakeFunc(typ, func(args []reflect.Value) []reflect.Value {
|
||||
r := strings.Repeat(args[0].String(), int(args[1].Int()))
|
||||
return []reflect.Value{reflect.ValueOf(r)}
|
||||
})
|
||||
r := fn.Interface().(func(string, int) string)("abc", 2)
|
||||
if r != "abcabc" {
|
||||
panic("error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user