本文整理汇总了Golang中reflect.Type.NumOut方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.NumOut方法的具体用法?Golang Type.NumOut怎么用?Golang Type.NumOut使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.NumOut方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: typeString_FuncOrMethod
func typeString_FuncOrMethod(
name string,
t reflect.Type,
pkgPath string) (s string) {
// Deal with input types.
var in []string
for i := 0; i < t.NumIn(); i++ {
in = append(in, typeString(t.In(i), pkgPath))
}
// And output types.
var out []string
for i := 0; i < t.NumOut(); i++ {
out = append(out, typeString(t.Out(i), pkgPath))
}
// Put it all together.
s = fmt.Sprintf(
"%s(%s) (%s)",
name,
strings.Join(in, ", "),
strings.Join(out, ", "))
return
}
示例2: OutTypes
func OutTypes(fnType reflect.Type) []reflect.Type {
var types []reflect.Type
for i := 0; i < fnType.NumOut(); i++ {
types = append(types, fnType.Out(i))
}
return types
}
示例3: sigMatches
// Note: Methods take the receiver as the first argument, which the want
// signature doesn't include.
func sigMatches(got, want reflect.Type) bool {
nin := want.NumIn()
if got.NumIn()-1 != nin {
return false
}
nout := want.NumOut()
if got.NumOut() != nout {
return false
}
for i := 0; i < nin; i++ {
if got.In(i+1) != want.In(i) {
return false
}
}
for i := 0; i < nout; i++ {
if got.Out(i) != want.Out(i) {
return false
}
}
return true
}
示例4: addImportsForType
// Add all necessary imports for the type, recursing as appropriate.
func addImportsForType(imports importMap, t reflect.Type) {
// Add any import needed for the type itself.
addImportForType(imports, t)
// Handle special cases where recursion is needed.
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
addImportsForType(imports, t.Elem())
case reflect.Func:
// Input parameters.
for i := 0; i < t.NumIn(); i++ {
addImportsForType(imports, t.In(i))
}
// Return values.
for i := 0; i < t.NumOut(); i++ {
addImportsForType(imports, t.Out(i))
}
case reflect.Map:
addImportsForType(imports, t.Key())
addImportsForType(imports, t.Elem())
}
}
示例5: verifyConversionFunctionSignature
// Verifies whether a conversion function has a correct signature.
func verifyConversionFunctionSignature(ft reflect.Type) error {
if ft.Kind() != reflect.Func {
return fmt.Errorf("expected func, got: %v", ft)
}
if ft.NumIn() != 3 {
return fmt.Errorf("expected three 'in' params, got: %v", ft)
}
if ft.NumOut() != 1 {
return fmt.Errorf("expected one 'out' param, got: %v", ft)
}
if ft.In(0).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 0, got: %v", ft)
}
if ft.In(1).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 1, got: %v", ft)
}
scopeType := Scope(nil)
if e, a := reflect.TypeOf(&scopeType).Elem(), ft.In(2); e != a {
return fmt.Errorf("expected '%v' arg for 'in' param 2, got '%v' (%v)", e, a, ft)
}
var forErrorType error
// This convolution is necessary, otherwise TypeOf picks up on the fact
// that forErrorType is nil.
errorType := reflect.TypeOf(&forErrorType).Elem()
if ft.Out(0) != errorType {
return fmt.Errorf("expected error return, got: %v", ft)
}
return nil
}
示例6: newTypeFromFactoryFunction
func newTypeFromFactoryFunction(function interface{}, factoryType reflect.Type, parameters []interface{}) TypeFactory {
if factoryType.NumOut() != 1 {
return newInvalidType(fmt.Errorf("invalid number of return parameters: %d", factoryType.NumOut()))
}
kindOfGeneratedType := factoryType.Out(0).Kind()
if kindOfGeneratedType != reflect.Interface && kindOfGeneratedType != reflect.Ptr {
return newInvalidType(fmt.Errorf("return parameter is no interface or pointer but a %v", kindOfGeneratedType))
}
if factoryType.IsVariadic() {
if factoryType.NumIn() > len(parameters) {
return newInvalidType(fmt.Errorf("invalid number of input parameters for variadic function: got %d but expected at least %d", len(parameters), factoryType.NumIn()))
}
} else {
if factoryType.NumIn() != len(parameters) {
return newInvalidType(fmt.Errorf("invalid number of input parameters: got %d but expected %d", len(parameters), factoryType.NumIn()))
}
}
t := &typeFactory{
factory: reflect.ValueOf(function),
factoryType: factoryType,
}
var err error
t.factoryArguments, err = buildFactoryCallArguments(factoryType, parameters)
if err != nil {
return newInvalidType(err)
}
return t
}
示例7: verifyHandler
// verifyHandler ensures that the given t is a function with the following signature:
// func(json.Context, *ArgType)(*ResType, error)
func verifyHandler(t reflect.Type) error {
if t.NumIn() != 2 || t.NumOut() != 2 {
return fmt.Errorf("handler should be of format func(json.Context, *ArgType) (*ResType, error)")
}
isStructPtr := func(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
isMap := func(t reflect.Type) bool {
return t.Kind() == reflect.Map && t.Key().Kind() == reflect.String
}
validateArgRes := func(t reflect.Type, name string) error {
if !isStructPtr(t) && !isMap(t) {
return fmt.Errorf("%v should be a pointer to a struct, or a map[string]interface{}", name)
}
return nil
}
if t.In(0) != typeOfContext {
return fmt.Errorf("arg0 should be of type json.Context")
}
if err := validateArgRes(t.In(1), "second argument"); err != nil {
return err
}
if err := validateArgRes(t.Out(0), "first return value"); err != nil {
return err
}
if !t.Out(1).AssignableTo(typeOfError) {
return fmt.Errorf("second return value should be an error")
}
return nil
}
示例8: checkFunc
func checkFunc(fnType reflect.Type) (bool, error) {
var state State
foundState := 0
nin := fnType.NumIn()
for i := 0; i < nin; i++ {
if fnType.In(i) == reflect.TypeOf(state) {
foundState++
} else if fnType.In(i) == reflect.TypeOf(&state) {
return false, fmt.Errorf("raw function can not use `*State' as arg, instead using `State'")
}
}
wrongRawFunc := false
if foundState > 1 {
wrongRawFunc = true
} else if foundState == 1 {
nout := fnType.NumOut()
if nin != 1 || nout != 1 {
wrongRawFunc = true
} else {
if fnType.Out(0).Kind() != reflect.Int {
wrongRawFunc = true
}
}
}
if wrongRawFunc {
return false, fmt.Errorf("raw function must be type: `func(State) int'")
}
return true, nil
}
示例9: makeClosure
func makeClosure(fv reflect.Value, ft reflect.Type) *function {
fn := &function{
call: fv,
}
var rt = Void
var at []Type
if n := ft.NumOut(); n != 0 {
rt = makeRetType(reflect.New(ft.Out(0)))
}
if n := ft.NumIn(); n != 0 {
at = make([]Type, n)
for i := 0; i != n; i++ {
at[i] = makeArgType(reflect.Zero(ft.In(i)))
}
}
fn.Interface = Prepare(rt, at...)
if err := constructClosure(fn); err != nil {
panic(err)
}
runtime.SetFinalizer(fn, destroyClosure)
return fn
}
示例10: methSigMatches
func methSigMatches(got reflect.Type, _want interface{}) error {
// Note: Methods take the receiver as the first argument, which the want
// signature doesn't include.
if got.NumIn() == 0 {
// The receiver is missing!
return fmt.Errorf("Method without reciever!")
}
want := reflect.TypeOf(_want)
if got.NumIn()-1 != want.NumIn() {
return fmt.Errorf("Method should have %d arguments, not %d", want.NumIn(), got.NumIn()-1)
}
if got.NumOut() != want.NumOut() {
return fmt.Errorf("Method should have %d return values, not %d", want.NumOut(), got.NumOut())
}
for i := 0; i < want.NumIn(); i++ {
if got.In(i+1) != want.In(i) {
return fmt.Errorf("Method argument %d should be %v, not %v", i+1, want.In(i), got.In(i+1))
}
}
for i := 0; i < want.NumOut(); i++ {
if got.Out(i) != want.Out(i) {
return fmt.Errorf("Method return value %d should be %v, not %v", i+1, want.Out(i), got.Out(i))
}
}
return nil
}
示例11: FormatFuncArguments
func FormatFuncArguments(t reflect.Type) (decl string) {
decl = "("
in := make([]string, t.NumIn())
for i := range in {
in[i] = ValType(t.In(i))
}
decl += strings.Join(in, ",") + ")"
out := make([]string, t.NumOut())
if len(out) > 0 {
for i := range out {
out[i] = ValType(t.Out(i))
}
s := strings.Join(out, ",")
if len(out) != 1 {
s = "(" + s + ")"
}
decl += " " + s
}
return
}
示例12: hasLastError
func hasLastError(t reflect.Type) bool {
N := t.NumOut()
if N == 0 {
return false
}
return t.Out(N-1) == errorType
}
示例13: verifyDeepCopyFunctionSignature
// Verifies whether a deep-copy function has a correct signature.
func verifyDeepCopyFunctionSignature(ft reflect.Type) error {
if ft.Kind() != reflect.Func {
return fmt.Errorf("expected func, got: %v", ft)
}
if ft.NumIn() != 3 {
return fmt.Errorf("expected three 'in' params, got $v", ft)
}
if ft.NumOut() != 1 {
return fmt.Errorf("expected one 'out' param, got %v", ft)
}
if ft.In(1).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 1, got: %v", ft)
}
if ft.In(1).Elem() != ft.In(0) {
return fmt.Errorf("expected 'in' param 0 the same as param 1, got: %v", ft)
}
var forClonerType Cloner
if expected := reflect.TypeOf(&forClonerType); ft.In(2) != expected {
return fmt.Errorf("expected '%v' arg for 'in' param 2, got: '%v'", expected, ft.In(2))
}
var forErrorType error
// This convolution is necessary, otherwise TypeOf picks up on the fact
// that forErrorType is nil
errorType := reflect.TypeOf(&forErrorType).Elem()
if ft.Out(0) != errorType {
return fmt.Errorf("expected error return, got: %v", ft)
}
return nil
}
示例14: Prepare
func (ms *GobMarshaller) Prepare(name string, fn interface{}) (err error) {
var (
fT reflect.Type
)
// Gob needs to register type before encode/decode
if fT = reflect.TypeOf(fn); fT.Kind() != reflect.Func {
err = fmt.Errorf("fn is not a function but %v", fn)
return
}
reg := func(v reflect.Value) (err error) {
if !v.CanInterface() {
err = fmt.Errorf("Can't convert to value in input of %v for name:%v", fn, name)
return
}
gob.Register(v.Interface())
return
}
for i := 0; i < fT.NumIn(); i++ {
// create a zero value of the type of parameters
if err = reg(reflect.Zero(fT.In(i))); err != nil {
return
}
}
for i := 0; i < fT.NumOut(); i++ {
if err = reg(reflect.Zero(fT.Out(i))); err != nil {
return
}
}
return
}
示例15: returns
func returns(f reflect.Type) []reflect.Type {
out := make([]reflect.Type, f.NumOut())
for i := 0; i < f.NumOut(); i++ {
out[i] = f.Out(i)
}
return out
}