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


Golang Value.Method方法代码示例

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


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

示例1: CallFunc

func (r *ReflectRouter) CallFunc(context *HttpContext, funcName string, option *RouterOption) (returnValue interface{}, statusCode HttpStatus, err error) {

	if index, ok := r.checkFuncName[funcName]; ok {
		statusCode = Status200

		var controller reflect.Value

		if reflect.Invalid != option.RouterDataRefVal.Kind() {
			controller = option.RouterDataRefVal
		} else {
			controller = r.ctlRefVal
		}

		refMet := controller.Method(index)

		//	get params
		args := r.getFuncArgs(refMet.Type(), context)

		//	call method
		reVals := refMet.Call(args)

		if 0 != len(reVals) {
			returnValue = reVals[0].Interface()
		}

	} else {
		statusCode = Status404
		err = NewLeafveinError("(" + r.typestr + ") not found func name: " + funcName)
	}

	return
}
开发者ID:slowfei,项目名称:leafveingo,代码行数:32,代码来源:lv_reflect_router.go

示例2: lookup

// Evaluate interfaces and pointers looking for a value that can look up the name, via a
// struct field, method, or map key, and return the result of the lookup.
func (t *Template) lookup(st *state, v reflect.Value, name string) reflect.Value {
	for v != nil {
		typ := v.Type()
		if n := v.Type().NumMethod(); n > 0 {
			for i := 0; i < n; i++ {
				m := typ.Method(i)
				mtyp := m.Type
				if m.Name == name && mtyp.NumIn() == 1 && mtyp.NumOut() == 1 {
					if !isExported(name) {
						t.execError(st, t.linenum, "name not exported: %s in type %s", name, st.data.Type())
					}
					return v.Method(i).Call(nil)[0]
				}
			}
		}
		switch av := v.(type) {
		case *reflect.PtrValue:
			v = av.Elem()
		case *reflect.InterfaceValue:
			v = av.Elem()
		case *reflect.StructValue:
			if !isExported(name) {
				t.execError(st, t.linenum, "name not exported: %s in type %s", name, st.data.Type())
			}
			return av.FieldByName(name)
		case *reflect.MapValue:
			return av.Elem(reflect.NewValue(name))
		default:
			return nil
		}
	}
	return v
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:35,代码来源:template.go

示例3: collectMethods

func (s *Scrubber) collectMethods(v reflect.Value, path Path, callbackMap map[string]Path) {
	for i := 0; i < v.NumMethod(); i++ {
		if v.Type().Method(i).PkgPath == "" { // exported
			name := v.Type().Method(i).Name
			name = strings.ToLower(name[0:1]) + name[1:]
			s.registerCallback(v.Method(i), append(path, name), callbackMap)
		}
	}
}
开发者ID:GregWilson,项目名称:kite,代码行数:9,代码来源:scrub.go

示例4: methodByName

// TODO: delete when reflect's own MethodByName is released.
func methodByName(receiver reflect.Value, name string) (reflect.Value, bool) {
	typ := receiver.Type()
	for i := 0; i < typ.NumMethod(); i++ {
		if typ.Method(i).Name == name {
			return receiver.Method(i), true // This value includes the receiver.
		}
	}
	return zero, false
}
开发者ID:aubonbeurre,项目名称:gcc,代码行数:10,代码来源:exec.go

示例5: getValue

func (pgv *pgValue) getValue(v reflect.Value) reflect.Value {
	switch pgv.Source.(type) {
	case reflect.StructField:
		return v.FieldByIndex(pgv.Index)
	case reflect.Method:
		return v.Method(pgv.Index[0]).Call(nil)[0]
	default:
		panic("not reached")
	}
}
开发者ID:koheeba,项目名称:pg,代码行数:10,代码来源:types.go

示例6: init

func init() {
	var opcode Opcode
	var opcodeType reflect.Type = reflect.TypeOf(opcode)
	var opcodeValue reflect.Value = reflect.ValueOf(opcode)

	numMethods := opcodeType.NumMethod()
	for i := 0; i < numMethods; i++ {
		funcName := opcodeType.Method(i).Name
		funcValue := opcodeValue.Method(i)
		funcTable[funcName] = funcValue
	}
}
开发者ID:remogatto,项目名称:z80,代码行数:12,代码来源:z80.go

示例7: pushStructMethods

func (ctx *Context) pushStructMethods(obj int, t reflect.Type, v reflect.Value) {
	mCount := t.NumMethod()
	for i := 0; i < mCount; i++ {
		methodName := t.Method(i).Name
		if !isExported(methodName) {
			continue
		}

		ctx.PushGoFunction(v.Method(i).Interface())
		ctx.PutPropString(obj, nameToJavaScript(methodName))

	}
}
开发者ID:Archs,项目名称:go-candyjs,代码行数:13,代码来源:base.go

示例8: methods

// methods walks over a structure and scrubs its exported methods.
func (s *Scrubber) methods(rv reflect.Value, path Path, callbacks map[string]Path) {
	for i := 0; i < rv.NumMethod(); i++ {
		if rv.Type().Method(i).PkgPath == "" { // exported
			cb, ok := rv.Method(i).Interface().(func(*Partial))
			if !ok {
				continue
			}

			name := rv.Type().Method(i).Name
			name = strings.ToLower(name[0:1]) + name[1:]
			s.register(cb, append(path, name), callbacks)
		}
	}
}
开发者ID:yonglehou,项目名称:kite,代码行数:15,代码来源:scrub.go

示例9: iter

// Return the result of calling the Iter method on v, or nil.
func iter(v reflect.Value) *reflect.ChanValue {
	for j := 0; j < v.Type().NumMethod(); j++ {
		mth := v.Type().Method(j)
		fv := v.Method(j)
		ft := fv.Type().(*reflect.FuncType)
		// TODO(rsc): NumIn() should return 0 here, because ft is from a curried FuncValue.
		if mth.Name != "Iter" || ft.NumIn() != 1 || ft.NumOut() != 1 {
			continue
		}
		ct, ok := ft.Out(0).(*reflect.ChanType)
		if !ok || ct.Dir()&reflect.RecvDir == 0 {
			continue
		}
		return fv.Call(nil)[0].(*reflect.ChanValue)
	}
	return nil
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:18,代码来源:template.go

示例10: iter

// Return the result of calling the Iter method on v, or nil.
func iter(v reflect.Value) reflect.Value {
	for j := 0; j < v.Type().NumMethod(); j++ {
		mth := v.Type().Method(j)
		fv := v.Method(j)
		ft := fv.Type()
		// TODO(rsc): NumIn() should return 0 here, because ft is from a curried FuncValue.
		if mth.Name != "Iter" || ft.NumIn() != 1 || ft.NumOut() != 1 {
			continue
		}
		ct := ft.Out(0)
		if ct.Kind() != reflect.Chan ||
			ct.ChanDir()&reflect.RecvDir == 0 {
			continue
		}
		return fv.Call(nil)[0]
	}
	return reflect.Value{}
}
开发者ID:go-nosql,项目名称:golang,代码行数:19,代码来源:template.go

示例11: findHttpMethod

func findHttpMethod(rcti reflect.Type, rcvi reflect.Value) map[string][]filterMethod {
	rcvhm := make(map[string][]filterMethod)

	for i := 0; i < rcti.NumMethod(); i++ {
		mName := rcti.Method(i).Name
		if hpos := strings.Index(mName, "Http_"); hpos != -1 {
			hpMname := mName[hpos+len("Http_"):]
			if mpos := strings.Index(hpMname, "_"); mpos != -1 {
				httpMethod := hpMname[:mpos]
				//len("_") == 1
				objMethod := hpMname[mpos+1:]

				rcvhm[objMethod] = append(rcvhm[objMethod], filterMethod{
					method: httpMethod,
					rcvm:   rcvi.Method(i),
				})
			}
		}
	}

	return rcvhm
}
开发者ID:golangers,项目名称:webrouter,代码行数:22,代码来源:manager.go

示例12: lookup

// Evaluate interfaces and pointers looking for a value that can look up the name, via a
// struct field, method, or map key, and return the result of the lookup.
func (t *Template) lookup(st *state, v reflect.Value, name string) reflect.Value {
	for v.IsValid() {
		typ := v.Type()
		if n := v.Type().NumMethod(); n > 0 {
			for i := 0; i < n; i++ {
				m := typ.Method(i)
				mtyp := m.Type
				if m.Name == name && mtyp.NumIn() == 1 && mtyp.NumOut() == 1 {
					if !isExported(name) {
						t.execError(st, t.linenum, "name not exported: %s in type %s", name, st.data.Type())
					}
					return v.Method(i).Call(nil)[0]
				}
			}
		}
		switch av := v; av.Kind() {
		case reflect.Ptr:
			v = av.Elem()
		case reflect.Interface:
			v = av.Elem()
		case reflect.Struct:
			if !isExported(name) {
				t.execError(st, t.linenum, "name not exported: %s in type %s", name, st.data.Type())
			}
			return av.FieldByName(name)
		case reflect.Map:
			if v := av.MapIndex(reflect.ValueOf(name)); v.IsValid() {
				return v
			}
			return reflect.Zero(typ.Elem())
		default:
			return reflect.Value{}
		}
	}
	return v
}
开发者ID:go-nosql,项目名称:golang,代码行数:38,代码来源:template.go

示例13: newMethod

func newMethod(receiver reflect.Value, i int) *methodType {
	return &methodType{receiver.Method(i), receiver.Type().Method(i)}
}
开发者ID:evaluation-alex,项目名称:gosync,代码行数:3,代码来源:gocheck.go

示例14: getVarFun

// Funkcja zwraca wartosc zmiennej o podanej nazwie lub indeksie. Jesli zmienna
// jest funkcja, wczesniej wywoluje ja z podanymi argumentami. Funkcja
// przeprowadza niezbedne dereferencje tak aby zwrocic zmienna, ktora nie jest
// ani wskaznikiem, ani interfejsem lub zwrocic wskaznik lub interface do takiej
// zmiennej. Uwaga! Funkcja moze modyfikowac wartosci args!
func getVarFun(ctx, name reflect.Value, args []reflect.Value, fun bool) (
	ret reflect.Value, stat int) {

	if !ctx.IsValid() {
		stat = RUN_NIL_CTX
		return
	}

	// Dereferencja nazwy
	dereference(&name)

	// Dereferencja jesli kontekst jest interfejsem
	if ctx.Kind() == reflect.Interface {
		ctx = ctx.Elem()
	}

	// Jesli nazwa jest stringiem probujemy znalezc metode o tej nazwie
	if name.Kind() == reflect.String {
		tt := ctx.Type()
		//nm := tt.NumMethod()
		for ii := 0; ii < tt.NumMethod(); ii++ {
			method := tt.Method(ii)
			// Sprawdzamy zgodnosc nazwy metody oraz typu receiver'a
			if method.Name == name.String() && tt == method.Type.In(0) {
				stat = argsMatch(method.Type, args, 1)
				if stat != RUN_OK {
					return
				}
				// Zwracamy pierwsza wartosc zwrocona przez metode
				ctx = ctx.Method(ii).Call(args)[0]
				// Nie pozwalamy na dalsza analize nazwy
				name = reflect.Value{}
				// Nie pozwalamy na traktowanie zwroconej zmiennej jak funkcji
				args = nil
				fun = false
				break
			}
		}
	}

	// Jesli name zawiera wartosc operujemy na nazwanej zmiennej z kontekstu,
	// w przeciwnym razie operujemy na samym kontekscie jako zmiennej.
	if name.IsValid() {
		// Pelna dereferencja kontekstu
		dereference(&ctx)
		// Pobieramy wartosc
		switch ctx.Kind() {
		case reflect.Struct:
			switch name.Kind() {
			case reflect.String:
				// Zwracamy pole struktury o podanej nazwie
				ft, ok := ctx.Type().FieldByName(name.String())
				if !ok {
					stat = RUN_NOT_FOUND
					return
				}
				if ft.PkgPath != "" {
					stat = RUN_UNEXPORTED
					return
				}
				ctx = ctx.FieldByIndex(ft.Index)
			case reflect.Int:
				// Zwracamy pole sruktury o podanym indeksie
				fi := int(name.Int())
				if fi < 0 || fi >= ctx.NumField() {
					stat = RUN_INDEX_OOR
					return
				}
				if ctx.Type().Field(fi).PkgPath != "" {
					stat = RUN_UNEXPORTED
					return
				}
				ctx = ctx.Field(fi)
			default:
				stat = RUN_NOT_FOUND
				return
			}

		case reflect.Map:
			kt := ctx.Type().Key()
			if !name.Type().AssignableTo(kt) {
				stat = RUN_NOT_FOUND
			}
			ctx = ctx.MapIndex(name)
			if !ctx.IsValid() {
				stat = RUN_NOT_FOUND
				return
			}

		case reflect.Array, reflect.Slice:
			switch name.Kind() {
			case reflect.Int:
				// Zwracamy element tablicy o podanym indeksie
				ei := int(name.Int())
				if ei < 0 || ei >= ctx.Len() {
//.........这里部分代码省略.........
开发者ID:ungerik,项目名称:kasia.go,代码行数:101,代码来源:getvarfun.go

示例15: AppendValue

func (m *method) AppendValue(dst []byte, v reflect.Value, quote bool) []byte {
	mv := v.Method(m.Index).Call(nil)[0]
	return m.appender(dst, mv, quote)
}
开发者ID:housinganywhere,项目名称:pg,代码行数:4,代码来源:field.go


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