当前位置: 首页>>代码示例>>Golang>>正文


Golang reflect.FuncValue类代码示例

本文整理汇总了Golang中reflect.FuncValue的典型用法代码示例。如果您正苦于以下问题:Golang FuncValue类的具体用法?Golang FuncValue怎么用?Golang FuncValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了FuncValue类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: invokeByName

func invokeByName(obj interface{}, name string, in ...reflect.Value) []reflect.Value {
	ov := reflect.NewValue(obj)
	ot := ov.Type()
	var fn *reflect.FuncValue = nil
	for i := 0; i < ot.NumMethod(); i++ {
		if ot.Method(i).Name == name {
			fn = ot.Method(i).Func
			break
		}
	}
	if fn == nil {
		return []reflect.Value{reflect.NewValue(nil)}
	}
	params := make([]reflect.Value, len(in)+1)
	params[0] = ov
	for i := 1; i < len(params); i++ {
		params[i] = in[i-1]
	}
	return fn.Call(params)
}
开发者ID:chanwit,项目名称:korat-lang,代码行数:20,代码来源:parser_test.go

示例2: safelyCall

//Calls a function with recover block
func (s *Server) safelyCall(function *reflect.FuncValue, args []reflect.Value) (resp []reflect.Value, e interface{}) {
	defer func() {
		if err := recover(); err != nil {
			if !s.Config.RecoverPanic {
				// go back to panic
				panic(err)
			} else {
				e = err
				resp = nil
				s.Logger.Println("Handler crashed with error", err)
				for i := 1; ; i += 1 {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					}
					s.Logger.Println(file, line)
				}
			}
		}
	}()
	return function.Call(args), nil
}
开发者ID:kylelemons,项目名称:web.go,代码行数:23,代码来源:web.go

示例3: newThread

func newThread(route []interface{}) *Thread {
	if len(route) == 0 {
		return nil
	}
	first := reflect.NewValue(route[0])
	args := make([]reflect.Value, 0, 8)
	canStop := true
	ok := false
	var fv *reflect.FuncValue
	switch first.(type) {
	case *reflect.PtrValue:
		{
			if len(route) < 2 {
				return nil
			}
			if str, typeok := route[1].(string); typeok {
				fv = getMemFunc(first.(*reflect.PtrValue), str)
				typ := fv.Type().(*reflect.FuncType)
				needArgs := typ.NumIn()
				actualArgs := len(route) - 1
				if needArgs == actualArgs {
					ok = true
					canStop = false
				} else if needArgs == actualArgs+1 {
					canStop = true
					ok = checkLastArg(typ)
				}
				if ok {
					args = append(args, first)
					for _, i := range route[2:] {
						args = append(args, reflect.NewValue(i))
					}
				}
			}
		}
	case *reflect.FuncValue:
		{
			fv = first.(*reflect.FuncValue)
			typ := fv.Type().(*reflect.FuncType)
			needArgs := typ.NumIn()
			actualArgs := len(route) - 1
			if needArgs == actualArgs {
				ok = true
				canStop = false
			} else if needArgs == actualArgs+1 {
				canStop = true
				ok = checkLastArg(typ)
			}
			if ok {
				for _, i := range route[1:] {
					args = append(args, reflect.NewValue(i))
				}
			}
		}
	}

	if ok {
		t := new(Thread)
		t.fv = fv
		t.canStop = canStop
		t.args = args
		return t
	}
	return nil
}
开发者ID:gaxxx,项目名称:funx,代码行数:65,代码来源:thread.go

示例4: tryOneFunction

// tryOneFunction is the common code for tryMethod and tryFunction.
func tryOneFunction(pkg, firstArg, name string, typ *reflect.FuncType, rfn *reflect.FuncValue, args []interface{}) {
	// Any results?
	if typ.NumOut() == 0 {
		return // Nothing to do.
	}
	// Right number of arguments + results?
	if typ.NumIn()+typ.NumOut() != len(args) {
		return
	}
	// Right argument and result types?
	for i, a := range args {
		if i < typ.NumIn() {
			if !compatible(a, typ.In(i)) {
				return
			}
		} else {
			if !compatible(a, typ.Out(i-typ.NumIn())) {
				return
			}
		}
	}
	// Build the call args.
	argsVal := make([]reflect.Value, typ.NumIn()+typ.NumOut())
	for i, a := range args {
		argsVal[i] = reflect.NewValue(a)
	}
	// Call the function and see if the results are as expected.
	resultVal := rfn.Call(argsVal[:typ.NumIn()])
	for i, v := range resultVal {
		if !reflect.DeepEqual(v.Interface(), args[i+typ.NumIn()]) {
			return
		}
	}
	// Present the result including a godoc command to get more information.
	firstIndex := 0
	if firstArg != "" {
		fmt.Fprintf(output, "%s.%s(", firstArg, name)
		firstIndex = 1
	} else {
		fmt.Fprintf(output, "%s.%s(", pkg, name)
	}
	for i := firstIndex; i < typ.NumIn(); i++ {
		if i > firstIndex {
			fmt.Fprint(output, ", ")
		}
		fmt.Fprintf(output, "%#v", args[i])
	}
	fmt.Fprint(output, ") = ")
	if typ.NumOut() > 1 {
		fmt.Fprint(output, "(")
	}
	for i := 0; i < typ.NumOut(); i++ {
		if i > 0 {
			fmt.Fprint(output, ", ")
		}
		fmt.Fprintf(output, "%#v", resultVal[i].Interface())
	}
	if typ.NumOut() > 1 {
		fmt.Fprint(output, ")")
	}
	fmt.Fprintf(output, "  // godoc %s %s\n", pkg, name)
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:63,代码来源:try.go

示例5: logArgPanic

func (c *C) logArgPanic(funcValue *reflect.FuncValue, expectedType string) {
	c.logf("... Panic: %s argument should be %s",
		niceFuncName(funcValue.Get()), expectedType)
}
开发者ID:nuin,项目名称:ampify,代码行数:4,代码来源:gocheck.go


注:本文中的reflect.FuncValue类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。