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


Golang Named.String方法代码示例

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


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

示例1: TypeStart

// Type definitions are only carried through to Haxe to allow access to objects as if they were native Haxe classes.
// TODO consider renaming
func (l langType) TypeStart(nt *types.Named, err string) string {
	typName := "GoType" + l.LangName("", nt.String())
	hxTyp := l.LangType(nt.Obj().Type(), false, nt.String())
	ret := ""
	switch hxTyp {
	case "Object":
		ret += "class " + typName
		ret += " extends " + hxTyp + " {\n"
	default:
		ret += "abstract " + typName + "(" + hxTyp + ") from " + hxTyp + " to " + hxTyp + " {\n"
	}
	switch nt.Underlying().(type) {
	case *types.Struct:
		str := nt.Underlying().(*types.Struct)
		ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n"
		flds := []string{}
		for f := 0; f < str.NumFields(); f++ {
			fName := str.Field(f).Name()
			if len(fName) > 0 {
				if unicode.IsUpper(rune(fName[0])) {
					flds = append(flds, fName)
				}
			}
		}
		sort.Strings(flds) // make sure the fields are always in the same order in the file
		for _, fName := range flds {
			for f := 0; f < str.NumFields(); f++ {
				if fName == str.Field(f).Name() {
					haxeTyp := l.LangType(str.Field(f).Type(), false, nt.String())
					fOff := fieldOffset(str, f)
					sfx := loadStoreSuffix(str.Field(f).Type(), true)
					ret += fmt.Sprintf("public var _%s(get,set):%s;\n", fName, haxeTyp)
					ret += fmt.Sprintf("function get__%s():%s { return get%s%d); }\n",
						fName, haxeTyp, sfx, fOff)
					ret += fmt.Sprintf("function set__%s(v:%s):%s { return set%s%d,v); }\n",
						fName, haxeTyp, haxeTyp, sfx, fOff)
					break
				}
			}
		}
	case *types.Array:
		ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n"
	default: // TODO not yet sure how to handle named types that are not structs
		ret += "inline public function new(v:" + hxTyp + ") { this = v; }\n"
	}

	meths := []string{}
	for m := 0; m < nt.NumMethods(); m++ {
		mName := nt.Method(m).Name()
		if len(mName) > 0 {
			if unicode.IsUpper(rune(mName[0])) {
				meths = append(meths, mName)
			}
		}
	}
	sort.Strings(meths) // make sure the methods always appear in the same order in the file
	for _, mName := range meths {
		for m := 0; m < nt.NumMethods(); m++ {
			meth := nt.Method(m)
			if mName == meth.Name() {
				sig := meth.Type().(*types.Signature)
				ret += "// " + mName + " " + sig.String() + "\n"
				ret += "public function _" + mName + "("
				for p := 0; p < sig.Params().Len(); p++ {
					if p > 0 {
						ret += ","
					}
					ret += "_" + sig.Params().At(p).Name() + ":" + l.LangType(sig.Params().At(p).Type(), false, nt.String())
				}
				ret += ")"
				switch sig.Results().Len() {
				case 0:
					ret += ":Void "
				case 1:
					ret += ":" + l.LangType(sig.Results().At(0).Type(), false, nt.String())
				default:
					ret += ":{"
					for r := 0; r < sig.Results().Len(); r++ {
						if r > 0 {
							ret += ","
						}
						ret += fmt.Sprintf("r%d:%s", r, l.LangType(sig.Results().At(r).Type(), false, nt.String()))
					}
					ret += "}"
				}
				ret += "{\n\t"
				if sig.Results().Len() > 0 {
					ret += "return "
				}
				fnToCall := l.LangName(
					nt.Obj().Pkg().Name()+":"+sig.Recv().Type().String(),
					meth.Name())
				ret += `Go_` + fnToCall + `.hx(this`
				for p := 0; p < sig.Params().Len(); p++ {
					ret += ", _" + sig.Params().At(p).Name()
				}
				ret += ");\n}\n"
			}
//.........这里部分代码省略.........
开发者ID:henrylee2cn,项目名称:tardisgo,代码行数:101,代码来源:types.go


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