本文整理汇总了Golang中reflect.Type.Out方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Out方法的具体用法?Golang Type.Out怎么用?Golang Type.Out使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.Out方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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())
}
}
示例3: 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
}
示例4: 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
}
示例5: 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
}
示例6: checkHandleType
func checkHandleType(t reflect.Type) (*requestType, error) {
if t.Kind() != reflect.Func {
return nil, errgo.New("not a function")
}
if n := t.NumIn(); n != 1 && n != 2 {
return nil, errgo.Newf("has %d parameters, need 1 or 2", t.NumIn())
}
if t.NumOut() > 2 {
return nil, errgo.Newf("has %d result parameters, need 0, 1 or 2", t.NumOut())
}
if t.NumIn() == 2 {
if t.In(0) != paramsType {
return nil, errgo.Newf("first argument is %v, need httprequest.Params", t.In(0))
}
} else {
if t.In(0) == paramsType {
return nil, errgo.Newf("no argument parameter after Params argument")
}
}
pt, err := getRequestType(t.In(t.NumIn() - 1))
if err != nil {
return nil, errgo.Notef(err, "last argument cannot be used for Unmarshal")
}
if t.NumOut() > 0 {
// func(p Params, arg *ArgT) error
// func(p Params, arg *ArgT) (ResultT, error)
if et := t.Out(t.NumOut() - 1); et != errorType {
return nil, errgo.Newf("final result parameter is %s, need error", et)
}
}
return pt, nil
}
示例7: 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
}
示例8: 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
}
示例9: 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
}
示例10: 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
}
示例11: hasLastError
func hasLastError(t reflect.Type) bool {
N := t.NumOut()
if N == 0 {
return false
}
return t.Out(N-1) == errorType
}
示例12: IsEqual
func (p *MethodMetadata) IsEqual(t reflect.Type) bool {
if t.ConvertibleTo(p.Method.Type) {
return false
}
baseIndex := 0
if p.Method.Index >= 0 {
baseIndex = 1
}
if t.NumIn()+baseIndex != p.Method.Type.NumIn() {
return false
}
for i := 0; i < p.Method.Type.NumIn()-baseIndex; i++ {
if p.Method.Type.In(baseIndex+i) != t.In(i) {
return false
}
}
for i := 0; i < p.Method.Type.NumOut(); i++ {
if p.Method.Type.Out(baseIndex+i) != t.Out(i) {
return false
}
}
return true
}
示例13: 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
}
示例14: 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
}
示例15: buildInvokeResult
// A version of Invoke that does error checking, used by both public methods.
func (a *returnAction) buildInvokeResult(
sig reflect.Type) (res []interface{}, err error) {
// Check the length of the return value.
numOut := sig.NumOut()
numVals := len(a.returnVals)
if numOut != numVals {
err = errors.New(
fmt.Sprintf("Return given %d vals; expected %d.", numVals, numOut))
return
}
// Attempt to coerce each return value.
res = make([]interface{}, numOut)
for i, val := range a.returnVals {
resType := sig.Out(i)
res[i], err = a.coerce(val, resType)
if err != nil {
res = nil
err = errors.New(fmt.Sprintf("Return: arg %d: %v", i, err))
return
}
}
return
}