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


Golang Type.String方法代码示例

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


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

示例1: encodeType

func encodeType(t types.Type) Type {
	t = t.Underlying()
	switch t.(type) {
	case *types.Basic:
		b := t.(*types.Basic)
		untyped := (b.Info() & types.IsUntyped) != 0
		return NewBasic(basicKindString(b), untyped)
	case *types.Pointer:
		p := t.(*types.Pointer)
		pt := encodeType(p.Elem())
		return NewPointer(pt)
	case *types.Array:
		a := t.(*types.Array)
		at := encodeType(a.Elem())
		return NewArray(at, a.Len())
	case *types.Slice:
		s := t.(*types.Slice)
		st := encodeType(s.Elem())
		return NewSlice(st)
	case *types.Signature:
		sig := t.(*types.Signature)
		v := sig.Recv()
		var vt *Type
		if v != nil {
			t := encodeType(v.Type())
			vt = &t
		}
		return NewSignature(
			sig.Variadic(),
			vt,
			tupleToSlice(sig.Params()),
			tupleToSlice(sig.Results()))
	case *types.Named:
		n := t.(*types.Named)
		return NewNamed(
			n.Obj().Pkg().Name(),
			n.Obj().Name(),
			n.Underlying())
	case *types.Interface:
		i := t.(*types.Interface)
		if i.Empty() {
			return NewInterface()
		} else {
			return NewUnsupported("Interfaces")
		}
	case *types.Tuple:
		return NewUnsupported("Tuples")
	case *types.Map:
		return NewUnsupported("Maps")
	case *types.Chan:
		return NewUnsupported("Channels")
	case *types.Struct:
		return NewUnsupported("Structs")
	default:
		return NewUnsupported(t.String())
	}
}
开发者ID:jasonkuhrt,项目名称:oden,代码行数:57,代码来源:objects.go

示例2: indirectType

// indirect(typ) assumes that typ is a pointer type,
// or named alias thereof, and returns its base type.
// Panic ensures if it is not a pointer.
//
func indirectType(ptr types.Type) types.Type {
	if v, ok := underlyingType(ptr).(*types.Pointer); ok {
		return v.Base
	}
	// When debugging it is convenient to comment out this line
	// and let it continue to print the (illegal) SSA form.
	panic("indirect() of non-pointer type: " + ptr.String())
	return nil
}
开发者ID:tj90241,项目名称:mips-baremetal,代码行数:13,代码来源:util.go

示例3: makeImplementsType

func makeImplementsType(T types.Type, fset *token.FileSet) serial.ImplementsType {
	var pos token.Pos
	if nt, ok := deref(T).(*types.Named); ok { // implementsResult.t may be non-named
		pos = nt.Obj().Pos()
	}
	return serial.ImplementsType{
		Name: T.String(),
		Pos:  fset.Position(pos).String(),
		Kind: typeKind(T),
	}
}
开发者ID:ChloeTigre,项目名称:golang-tools,代码行数:11,代码来源:implements.go

示例4: newVar

func newVar(p *Package, typ types.Type, objname, name, doc string) *Var {
	sym := p.syms.symtype(typ)
	if sym == nil {
		panic(fmt.Errorf("could not find symbol for type [%s]!", typ.String()))
	}
	return &Var{
		pkg:  p,
		sym:  sym,
		id:   p.Name() + "_" + objname,
		doc:  doc,
		name: name,
	}
}
开发者ID:ashrafulratul,项目名称:gopy,代码行数:13,代码来源:vars.go

示例5: validResultType

func (f *File) validResultType(typ types.Type) *Error {
	switch typ.(type) {
	default:
		name := typ.String()
		if named, ok := typ.(*types.Named); ok {
			name = named.Obj().Name()
		}
		return &Error{errors.New(fmt.Sprint("Invalid result type:", name)), 0}
	case *types.Basic:
		typ := typ.(*types.Basic)
		switch typ.Kind() {
		default:

			return &Error{errors.New(fmt.Sprint("Invalid basic type for result type :", typ.Info())), 0}
		case types.Bool:
			return nil
		case types.Int:
			return nil
		case types.Int8:
			return nil
		case types.Int16:
			return nil
		case types.Int32:
			return nil
		case types.Int64:
			return nil
		case types.Uint:
			return nil
		case types.Uint8:
			return nil
		case types.Uint16:
			return nil
		case types.Uint32:
			return nil
		case types.Uint64:
			return nil
		case types.Float32:
			return nil
		case types.Float64:
			return nil
		}
	}
}
开发者ID:bjwbell,项目名称:gensimd,代码行数:43,代码来源:parse.go

示例6: writeSignature

// writeSignature writes to w the signature sig in declaration syntax.
// Derived from types.Signature.String().
//
func writeSignature(w io.Writer, name string, sig *types.Signature, params []*Parameter) {
	io.WriteString(w, "func ")
	if sig.Recv != nil {
		io.WriteString(w, "(")
		if n := params[0].Name(); n != "" {
			io.WriteString(w, n)
			io.WriteString(w, " ")
		}
		io.WriteString(w, params[0].Type().String())
		io.WriteString(w, ") ")
		params = params[1:]
	}
	io.WriteString(w, name)
	io.WriteString(w, "(")
	for i, v := range params {
		if i > 0 {
			io.WriteString(w, ", ")
		}
		io.WriteString(w, v.Name())
		io.WriteString(w, " ")
		if sig.IsVariadic && i == len(params)-1 {
			io.WriteString(w, "...")
			io.WriteString(w, underlyingType(v.Type()).(*types.Slice).Elt.String())
		} else {
			io.WriteString(w, v.Type().String())
		}
	}
	io.WriteString(w, ")")
	if res := sig.Results; res != nil {
		io.WriteString(w, " ")
		var t types.Type
		if len(res) == 1 && res[0].Name == "" {
			t = res[0].Type
		} else {
			t = &types.Result{Values: res}
		}
		io.WriteString(w, t.String())
	}
}
开发者ID:serge-hulne,项目名称:golang,代码行数:42,代码来源:func.go

示例7: typeString

// typeString returns a string representation of n.
func typeString(typ types.Type) string {
	return filepath.ToSlash(typ.String())
}
开发者ID:Harvey-OS,项目名称:go,代码行数:4,代码来源:fmt_test.go

示例8: appendComponentsRecursive

// appendComponentsRecursive implements componentsOfType.
// Recursion is required to correct handle structs and arrays,
// which can contain arbitrary other types.
func appendComponentsRecursive(arch *asmArch, t types.Type, cc []component, suffix string, off int) []component {
	s := t.String()
	size := int(arch.sizes.Sizeof(t))
	kind := asmKindForType(t, size)
	cc = append(cc, newComponent(suffix, kind, s, off, size, suffix))

	switch kind {
	case 8:
		if arch.ptrSize() == 4 {
			w1, w2 := "lo", "hi"
			if arch.bigEndian {
				w1, w2 = w2, w1
			}
			cc = append(cc, newComponent(suffix+"_"+w1, 4, "half "+s, off, 4, suffix))
			cc = append(cc, newComponent(suffix+"_"+w2, 4, "half "+s, off+4, 4, suffix))
		}

	case asmEmptyInterface:
		cc = append(cc, newComponent(suffix+"_type", asmKind(arch.ptrSize()), "interface type", off, arch.ptrSize(), suffix))
		cc = append(cc, newComponent(suffix+"_data", asmKind(arch.ptrSize()), "interface data", off+arch.ptrSize(), arch.ptrSize(), suffix))

	case asmInterface:
		cc = append(cc, newComponent(suffix+"_itable", asmKind(arch.ptrSize()), "interface itable", off, arch.ptrSize(), suffix))
		cc = append(cc, newComponent(suffix+"_data", asmKind(arch.ptrSize()), "interface data", off+arch.ptrSize(), arch.ptrSize(), suffix))

	case asmSlice:
		cc = append(cc, newComponent(suffix+"_base", asmKind(arch.ptrSize()), "slice base", off, arch.ptrSize(), suffix))
		cc = append(cc, newComponent(suffix+"_len", asmKind(arch.intSize()), "slice len", off+arch.ptrSize(), arch.intSize(), suffix))
		cc = append(cc, newComponent(suffix+"_cap", asmKind(arch.intSize()), "slice cap", off+arch.ptrSize()+arch.intSize(), arch.intSize(), suffix))

	case asmString:
		cc = append(cc, newComponent(suffix+"_base", asmKind(arch.ptrSize()), "string base", off, arch.ptrSize(), suffix))
		cc = append(cc, newComponent(suffix+"_len", asmKind(arch.intSize()), "string len", off+arch.ptrSize(), arch.intSize(), suffix))

	case asmComplex:
		fsize := size / 2
		cc = append(cc, newComponent(suffix+"_real", asmKind(fsize), fmt.Sprintf("real(complex%d)", size*8), off, fsize, suffix))
		cc = append(cc, newComponent(suffix+"_imag", asmKind(fsize), fmt.Sprintf("imag(complex%d)", size*8), off+fsize, fsize, suffix))

	case asmStruct:
		tu := t.Underlying().(*types.Struct)
		fields := make([]*types.Var, tu.NumFields())
		for i := 0; i < tu.NumFields(); i++ {
			fields[i] = tu.Field(i)
		}
		offsets := arch.sizes.Offsetsof(fields)
		for i, f := range fields {
			cc = appendComponentsRecursive(arch, f.Type(), cc, suffix+"_"+f.Name(), off+int(offsets[i]))
		}

	case asmArray:
		tu := t.Underlying().(*types.Array)
		elem := tu.Elem()
		// Calculate offset of each element array.
		fields := []*types.Var{
			types.NewVar(token.NoPos, nil, "fake0", elem),
			types.NewVar(token.NoPos, nil, "fake1", elem),
		}
		offsets := arch.sizes.Offsetsof(fields)
		elemoff := int(offsets[1])
		for i := 0; i < int(tu.Len()); i++ {
			cc = appendComponentsRecursive(arch, elem, cc, suffix+"_"+strconv.Itoa(i), i*elemoff)
		}
	}

	return cc
}
开发者ID:Harvey-OS,项目名称:go,代码行数:70,代码来源:asmdecl.go

示例9: encodeType

func encodeType(t types.Type) Type {
	// First see if it's a Named type. If so, wrap in Named and recurse with the
	// underlying type.
	if n, isNamed := t.(*types.Named); isNamed {

		// When n.Obj().Pkg() is nil the Named type is defined in the universe. We
		// represent that with an empty package name slice.
		var pkgSegments []string = []string{}
		if n.Obj().Pkg() != nil {
			pkgSegments = strings.Split(n.Obj().Pkg().Path(), "/")
		}
		return NewNamed(
			pkgSegments,
			n.Obj().Name(),
			encodeType(n.Underlying()))
	}

	switch t.Underlying().(type) {
	case *types.Basic:
		b := t.(*types.Basic)
		untyped := (b.Info() & types.IsUntyped) != 0
		return NewBasic(basicKindString(b), untyped)
	case *types.Pointer:
		p := t.(*types.Pointer)
		pt := encodeType(p.Elem())
		return NewPointer(pt)
	case *types.Array:
		a := t.(*types.Array)
		at := encodeType(a.Elem())
		return NewArray(at, a.Len())
	case *types.Slice:
		s := t.(*types.Slice)
		st := encodeType(s.Elem())
		return NewSlice(st)
	case *types.Signature:
		sig := t.(*types.Signature)
		v := sig.Recv()
		var vt *Type
		if v != nil {
			t := encodeType(v.Type())
			vt = &t
		}
		return NewSignature(
			sig.Variadic(),
			vt,
			tupleToSlice(sig.Params()),
			tupleToSlice(sig.Results()))
	case *types.Interface:
		i := t.(*types.Interface)
		if i.Empty() {
			return NewInterface()
		} else {
			return NewUnsupported("Interfaces")
		}
	case *types.Struct:
		s := t.(*types.Struct)
		fields := []StructField{}
		for i := 0; i < s.NumFields(); i++ {
			f := s.Field(i)
			fields = append(fields, NewStructField(f.Name(), encodeType(f.Type())))
		}
		return NewStruct(fields)
	case *types.Tuple:
		return NewUnsupported("Tuples")
	case *types.Map:
		return NewUnsupported("Maps")
	case *types.Chan:
		return NewUnsupported("Channels")
	default:
		return NewUnsupported(t.String())
	}
}
开发者ID:AlbinTheander,项目名称:oden,代码行数:72,代码来源:objects.go

示例10: walkType

// walkType adds the type, and any necessary child types.
func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *types.Type {
	// Most of the cases are underlying types of the named type.
	name := tcNameToName(in.String())
	if useName != nil {
		name = *useName
	}

	switch t := in.(type) {
	case *tc.Struct:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Struct
		for i := 0; i < t.NumFields(); i++ {
			f := t.Field(i)
			m := types.Member{
				Name:         f.Name(),
				Embedded:     f.Anonymous(),
				Tags:         t.Tag(i),
				Type:         b.walkType(u, nil, f.Type()),
				CommentLines: splitLines(b.priorCommentLines(f.Pos(), 1).Text()),
			}
			out.Members = append(out.Members, m)
		}
		return out
	case *tc.Map:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Map
		out.Elem = b.walkType(u, nil, t.Elem())
		out.Key = b.walkType(u, nil, t.Key())
		return out
	case *tc.Pointer:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Pointer
		out.Elem = b.walkType(u, nil, t.Elem())
		return out
	case *tc.Slice:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Slice
		out.Elem = b.walkType(u, nil, t.Elem())
		return out
	case *tc.Array:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Array
		out.Elem = b.walkType(u, nil, t.Elem())
		// TODO: need to store array length, otherwise raw type name
		// cannot be properly written.
		return out
	case *tc.Chan:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Chan
		out.Elem = b.walkType(u, nil, t.Elem())
		// TODO: need to store direction, otherwise raw type name
		// cannot be properly written.
		return out
	case *tc.Basic:
		out := u.Type(types.Name{
			Package: "",
			Name:    t.Name(),
		})
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Unsupported
		return out
	case *tc.Signature:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Func
		out.Signature = b.convertSignature(u, t)
		return out
	case *tc.Interface:
		out := u.Type(name)
		if out.Kind != types.Unknown {
			return out
		}
		out.Kind = types.Interface
		t.Complete()
		for i := 0; i < t.NumMethods(); i++ {
			if out.Methods == nil {
				out.Methods = map[string]*types.Type{}
//.........这里部分代码省略.........
开发者ID:ncdc,项目名称:kubernetes,代码行数:101,代码来源:parse.go

示例11: Extract

//Extract rpc services and methods by analysing type definitions
func (g *Generator) Extract() error {
	rpcmethods := map[string]*RPCMethod{}

	for _, obj := range g.pkg.defs {
		if obj == nil {
			continue
		}

	DEFSWITCH:
		switch t := obj.Type().(type) {

		//as per "net/rpc" are we looking for methods with the following:
		// [x] the fn is a method (has receiver)
		// [x] the method's type is exported.
		// [x] the method is exported.
		// [x] the method has two arguments...
		// [x] both exported (or builtin) types.
		// [x] the method's second argument is a pointer.
		// [x] the method has return type error.
		case *types.Signature:

			//needs to be a method (have a receiver) and be exported
			if t.Recv() == nil || !obj.Exported() {
				break
			}

			//receiver must be named and exported
			var recvt types.Type
			if recvpt, ok := t.Recv().Type().(*types.Pointer); ok {
				recvt = recvpt.Elem()
			} else {
				recvt = t.Recv().Type()
			}

			recv, ok := recvt.(*types.Named)
			if ok {
				if !recv.Obj().Exported() {
					break
				}
			}

			//method must have two params
			if t.Params().Len() != 2 {
				break
			}

			//all param types must be exported or builtin
			for i := 0; i < t.Params().Len(); i++ {
				var paramt types.Type
				if parampt, ok := t.Params().At(i).Type().(*types.Pointer); ok {
					paramt = parampt.Elem()
				} else {

					//second arg must be a pointer
					if i == 1 {
						break DEFSWITCH
					}

					paramt = t.Params().At(i).Type()
				}

				//if param type is Named, it must be exported, else it must be buildin
				if paramnamedt, ok := paramt.(*types.Named); ok {
					if !paramnamedt.Obj().Exported() {
						break DEFSWITCH
					}

				} else if strings.Contains(paramt.String(), ".") {
					break DEFSWITCH
				}
			}

			//must have one result: error
			if t.Results().Len() != 1 || t.Results().At(0).Type().String() != "error" {
				break
			}

			rpcmethods[obj.Name()] = &RPCMethod{
				input:  t.Params().At(0).Type(),
				output: t.Params().At(1).Type().(*types.Pointer).Elem(), //this is checked above
				recv:   recv,
				sig:    t,
			}
		}
	}

	for n, rpcm := range rpcmethods {
		rpcs, ok := g.services[rpcm.recv.Obj().Name()]
		if !ok {
			rpcs = &RPCService{
				methods: map[string]*RPCMethod{},
			}

			g.services[rpcm.recv.Obj().Name()] = rpcs
		}

		rpcs.methods[n] = rpcm
	}

//.........这里部分代码省略.........
开发者ID:advanderveer,项目名称:rpctocli,代码行数:101,代码来源:main.go

示例12: validType

func validType(T types.Type) bool {
	return T != nil &&
		T != types.Typ[types.Invalid] &&
		!strings.Contains(T.String(), "invalid type") // good but not foolproof
}
开发者ID:golang,项目名称:gddo,代码行数:5,代码来源:lint.go

示例13: hashType

// hashType returns a hash for t such that
// types.IsIdentical(x, y) => hashType(x) == hashType(y).
func hashType(t types.Type) int {
	return hashString(t.String()) // TODO(gri): provide a better hash
}
开发者ID:serge-hulne,项目名称:golang,代码行数:5,代码来源:value.go

示例14: Bar200

// Bar200 converts a type to a string.
func Bar200(t types.Type) string { return t.String() }
开发者ID:cweill,项目名称:gotests,代码行数:2,代码来源:test200.go

示例15: findMethod

func findMethod(prog *ssa.Program, meth *types.Func, typ types.Type) *ssa.Function {
	if meth != nil {
		fmt.Fprintf(os.Stderr, "     ^ finding method for type: %s pkg: %s name: %s\n", typ.String(), meth.Pkg().Name(), meth.Name())
	}
	return prog.LookupMethod(typ, meth.Pkg(), meth.Name())
}
开发者ID:nickng,项目名称:dingo-hunter,代码行数:6,代码来源:func.go


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