• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang printer.Fprint函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中go/printer.Fprint函数的典型用法代码示例。如果您正苦于以下问题:Golang Fprint函数的具体用法?Golang Fprint怎么用?Golang Fprint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: fprint

func fprint(w io.Writer, a interface{}) error {
	switch x := a.(type) {
	case *ast.FieldList:
		// printer.Fprint does not support this type. Hack around it.
		return fprintFieldList(w, x)
	case ast.Node, []ast.Decl, []ast.Stmt:
		return printer.Fprint(w, token.NewFileSet(), x)
	case []ast.Expr:
		i := 0
		for ; i < len(x)-1; i++ {
			if err := printer.Fprint(w, token.NewFileSet(), x[i]); err != nil {
				return err
			}
			if _, err := w.Write([]byte(", ")); err != nil {
				return err
			}
		}
		if len(x) != 0 {
			return printer.Fprint(w, token.NewFileSet(), x[i])
		}
		return nil
	case string:
		_, err := io.WriteString(w, x)
		return err
	default:
		panic(fmt.Sprintf("unsupported value: %v", x))
	}
}
开发者ID:philipmulcahy,项目名称:godebug,代码行数:28,代码来源:astutil.go


示例2: printIncReturns

func printIncReturns(fset *token.FileSet, v map[*ast.ReturnStmt]*ast.FuncType) {
	for ret, ftyp := range v {
		fmt.Print("FUNC TYPE: ")
		printer.Fprint(os.Stdout, fset, ftyp)
		fmt.Print("   RETURN: ")
		printer.Fprint(os.Stdout, fset, ret)
		fmt.Println()
	}
}
开发者ID:DuoSoftware,项目名称:v6engine-deps,代码行数:9,代码来源:fix.go


示例3: Do

// Do creates a concrete ast from the generic one and writes it to the given io.Writer
func (g *Generify) Do(packageName string, w io.Writer) error {
	var decls []ast.Decl
	g.genTypes, decls = findGenerics(g.file)
	if len(g.genTypes) == 0 {
		return fmt.Errorf("no generic types found")
	}
	if len(g.genTypes) != len(g.concreteTypes.Instance[0]) {
		return fmt.Errorf("there are %d generic types but %d concrete types", len(g.genTypes), len(g.concreteTypes.Instance[0]))
	}

	removeCommentsFrom(decls)

	decls = splitDeclsToUngroupedDecls(decls)

	g.genericDecls = g.inspectAllDeclsForDependencies(decls)

	g.checkMethodDependencies()

	g.renameStructsAndVars()

	g.renameFunctions()

	file := ast.File{Name: g.file.Name, Decls: g.staticDecls(), Scope: g.file.Scope, Imports: g.file.Imports}
	if packageName != "" {
		file.Name.Name = packageName
	}

	fset := token.NewFileSet()
	err := printer.Fprint(w, fset, &file)
	if err != nil {
		return err
	}
	w.Write(newline)

	for _, types := range g.concreteTypes.Instance {

		// rename all identifiers
		for _, ra := range g.renameActions {
			ra.rename(types)
		}

		// write the renamed ast
		for _, decl := range g.genericDecls {
			if len(decl.usedTypes) > 0 && !decl.isAllreadyWritten(types) {
				err := printer.Fprint(w, fset, decl.decl)
				if err != nil {
					return err
				}
				w.Write(newline)
			}
		}
	}

	return nil
}
开发者ID:hneemann,项目名称:yagi,代码行数:56,代码来源:generify.go


示例4: rangestr

func (v *visitor) rangestr(rs *ast.RangeStmt) string {
	v.buffer.Reset()
	v.buffer.WriteString("for ")
	printer.Fprint(&v.buffer, v.fset, rs.Key)
	if rs.Value != nil {
		v.buffer.WriteString(", ")
		printer.Fprint(&v.buffer, v.fset, rs.Value)
	}
	v.buffer.WriteString(" := range ")
	printer.Fprint(&v.buffer, v.fset, rs.X)
	return v.buffer.String()
}
开发者ID:evanj,项目名称:loopcheck,代码行数:12,代码来源:loopcheck.go


示例5: main

func main() {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "simple.go", nil, 0)
	if err != nil {
		// Whoops!
	}
	ast.Walk(new(FuncVisitor), file)
	printer.Fprint(os.Stdout, fset, file)
	fmt.Println("-------------------------------------")
	ast.Walk(new(ImportVisitor), file)
	printer.Fprint(os.Stdout, fset, file)

	f, _ := os.Create("/tmp/new_simple.go")
	printer.Fprint(f, fset, file)
}
开发者ID:AnuchitPrasertsang,项目名称:golang-ast-example,代码行数:15,代码来源:transformation.go


示例6: render

func (f *file) render(x interface{}) string {
	var buf bytes.Buffer
	if err := printer.Fprint(&buf, f.fset, x); err != nil {
		panic(err)
	}
	return buf.String()
}
开发者ID:swadhin4,项目名称:lint,代码行数:7,代码来源:lint.go


示例7: Process

// Process takes a Go source file and bumps version declaration according to conf.
// Returns the modified code and a map from identifiers to updated versions and an error, if any.
func (conf Config) Process(filename string, src interface{}) ([]byte, map[string]string, error) {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
	if err != nil {
		return nil, nil, err
	}

	versions, err := conf.ProcessNode(fset, file)
	if err != nil {
		return nil, nil, err
	}

	var buf bytes.Buffer
	err = printer.Fprint(&buf, fset, file)
	if err != nil {
		return nil, nil, err
	}

	out := buf.Bytes()

	out, err = format.Source(out)
	if err != nil {
		return nil, nil, err
	}

	return out, versions, nil
}
开发者ID:motemen,项目名称:gobump,代码行数:29,代码来源:gobump.go


示例8: render

func render(fset *token.FileSet, x interface{}) string {
	var buf bytes.Buffer
	if err := printer.Fprint(&buf, fset, x); err != nil {
		panic(err)
	}
	return buf.String()
}
开发者ID:hailocab,项目名称:golint,代码行数:7,代码来源:lint_test.go


示例9: astString

func astString(x ast.Expr) string {
	var buf bytes.Buffer
	if err := printer.Fprint(&buf, fset, x); err != nil {
		panic(err)
	}
	return buf.String()
}
开发者ID:tmc,项目名称:gen-mocks,代码行数:7,代码来源:gen_mocks.go


示例10: main

func main() {

	if filename == "" || line == -1 || column == -1 || newName == "" {
		fmt.Printf("%v %v %v \n", filename, line, column)
		fmt.Printf("Usage: goref -f <filename> -l <line number> -c <column number> -n <new symbol name>\n")
		return
	}
	newPack, count := RenameIdent(".", filename, line, column, newName)
	if newPack != nil {
		fmt.Printf("res : %v\n", count)
		for fname, f := range newPack.Files {
			if nf, err := os.OpenFile("refgo_out_"+fname, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
				fmt.Printf("file %v already exists,cannot finish output\n", "refgo_out_"+fname)
				return
			} else {

				printer.Fprint(nf, f)
				nf.Close()
			}
		}
	} else {
		fmt.Printf("error : %v\n", count)
	}

}
开发者ID:vpavkin,项目名称:GoRefactor,代码行数:25,代码来源:renameUtility.go


示例11: formatType

func (r *InterfaceGen) formatType(fileset *token.FileSet, field *ast.Field) *Type {
	var typeBuf bytes.Buffer
	_ = printer.Fprint(&typeBuf, fileset, field.Type)
	if len(field.Names) == 0 {
		fatalNode(fileset, field, "RPC interface parameters and results must all be named")
	}
	for _, typeName := range types(field.Type) {
		parts := strings.SplitN(typeName, ".", 2)
		if len(parts) > 1 {
			for _, imp := range r.CheckImports {
				importPath := imp.Path.Value[1 : len(imp.Path.Value)-1]
				if imp.Name != nil && imp.Name.String() == parts[0] {
					r.Imports[fmt.Sprintf("%s %s", imp.Name, importPath)] = true
				} else if filepath.Base(importPath) == parts[0] {
					r.Imports[importPath] = true
				}
			}
		}
	}
	t := &Type{Type: typeBuf.String()}
	for _, n := range field.Names {
		lowerName := n.Name
		name := strings.ToUpper(lowerName[0:1]) + lowerName[1:]
		t.Names = append(t.Names, name)
		t.LowerNames = append(t.LowerNames, lowerName)
	}
	return t
}
开发者ID:alecthomas,项目名称:go-rpcgen,代码行数:28,代码来源:go-rpcgen.go


示例12: extractFunc

func extractFunc(funcName string, inputPath string) ([]string, string, error) {
	fset := token.NewFileSet()

	fp, err := parser.ParseFile(fset, inputPath, nil, 0)

	if err != nil {
		return nil, "", err
	}

	imports := make([]string, 0)

	for _, imp := range fp.Imports {
		imports = append(imports, imp.Path.Value)
	}

	var buf bytes.Buffer

	for _, decl := range fp.Decls {
		f, ok := decl.(*ast.FuncDecl)
		if !ok {
			continue
		}

		if f.Name.Name != funcName {
			continue
		}

		f.Name.Name = "ffjson_" + f.Name.Name
		printer.Fprint(&buf, fset, f)
		break
	}

	return imports, string(buf.Bytes()), nil
}
开发者ID:klauspost,项目名称:ffjson,代码行数:34,代码来源:extract.go


示例13: walkFile

func (w *Walker) walkFile(name string, file *ast.File) {
	// Not entering a scope here; file boundaries aren't interesting.

	for _, di := range file.Decls {
		switch d := di.(type) {
		case *ast.GenDecl:
			switch d.Tok {
			case token.IMPORT:
				continue
			case token.CONST:
				for _, sp := range d.Specs {
					w.walkConst(sp.(*ast.ValueSpec))
				}
			case token.TYPE:
				for _, sp := range d.Specs {
					w.walkTypeSpec(sp.(*ast.TypeSpec))
				}
			case token.VAR:
				for _, sp := range d.Specs {
					w.walkVar(sp.(*ast.ValueSpec))
				}
			default:
				log.Fatalf("unknown token type %d in GenDecl", d.Tok)
			}
		case *ast.FuncDecl:
			// Ignore. Handled in subsequent pass, by go/doc.
		default:
			log.Printf("unhandled %T, %#v\n", di, di)
			printer.Fprint(os.Stderr, w.fset, di)
			os.Stderr.Write([]byte("\n"))
		}
	}
}
开发者ID:krasin,项目名称:go-deflate,代码行数:33,代码来源:goapi.go


示例14: main

func main() {
	if len(os.Args) > 1 {
		args(os.Args[1])
		return
	}

	r := bufio.NewReader(os.Stdin)

	for {
		fmt.Print(">> ")
		line, _, _ := r.ReadLine()
		p := parser.ParseFromString("<REPL>", string(line)+"\n")
		fmt.Println(p)

		// a := generator.GenerateAST(p)
		a := generator.EvalExprs(p)
		fset := token.NewFileSet()

		ast.Print(fset, a)

		var buf bytes.Buffer
		printer.Fprint(&buf, fset, a)
		fmt.Printf("%s\n", buf.String())
	}
}
开发者ID:8l,项目名称:gsp,代码行数:25,代码来源:gsp.go


示例15: args

func args(filename string) {
	b, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}

	p := parser.ParseFromString(filename, string(b)+"\n")

	a := generator.GenerateAST(p)

	fset := token.NewFileSet()

	defaultImports := []string{"github.com/gsp-lang/stdlib/prelude", "github.com/gsp-lang/gsp/core"}
	for _, defaultImport := range defaultImports {
		split := strings.Split(defaultImport, "/")
		pkgName := split[len(split)-1]
		if !(a.Name.Name == "prelude" && pkgName == "prelude") {
			astutil.AddImport(fset, a, defaultImport)
		}
	}

	if a.Name.Name != "prelude" {
		a.Decls = append(a.Decls, &ast.GenDecl{
			Tok: token.VAR,
			Specs: []ast.Spec{&ast.ValueSpec{
				Names:  []*ast.Ident{&ast.Ident{Name: "_"}},
				Values: []ast.Expr{&ast.Ident{Name: "prelude.Len"}},
			}},
		})
	}

	var buf bytes.Buffer
	printer.Fprint(&buf, fset, a)
	fmt.Printf("%s\n", buf.String())
}
开发者ID:8l,项目名称:gsp,代码行数:35,代码来源:gsp.go


示例16: ExampleFprint

func ExampleFprint() {
	// Parse source file and extract the AST without comments for
	// this function, with position information referring to the
	// file set fset.
	funcAST, fset := parseFunc("example_test.go", "ExampleFprint")

	// Print the function body into buffer buf.
	// The file set is provided to the printer so that it knows
	// about the original source formatting and can add additional
	// line breaks where they were present in the source.
	var buf bytes.Buffer
	printer.Fprint(&buf, fset, funcAST.Body)

	// Remove braces {} enclosing the function body, unindent,
	// and trim leading and trailing white space.
	s := buf.String()
	s = s[1 : len(s)-1]
	s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))

	// Print the cleaned-up body text to stdout.
	fmt.Println(s)

	// output:
	// funcAST, fset := parseFunc("example_test.go", "ExampleFprint")
	//
	// var buf bytes.Buffer
	// printer.Fprint(&buf, fset, funcAST.Body)
	//
	// s := buf.String()
	// s = s[1 : len(s)-1]
	// s = strings.TrimSpace(strings.Replace(s, "\n\t", "\n", -1))
	//
	// fmt.Println(s)
}
开发者ID:2thetop,项目名称:go,代码行数:34,代码来源:example_test.go


示例17: TestGenerateTestCase

func TestGenerateTestCase(t *testing.T) {
	fset := token.NewFileSet()
	srcFile := "/home/joar/go/src/github.com/joarleth/slask/slask.go"
	file, err := parser.ParseFile(fset, srcFile, nil, parser.ParseComments)

	if err != nil {
		t.Error(err)
	}

	mutationIDs := []uint{3, 7}

	for _, d := range file.Decls {
		if fd, ok := d.(*ast.FuncDecl); ok {
			if fd.Name.Name == "Join" {
				f, fset := GenerateTestCase(fd, mutationIDs, "slask")

				var buf bytes.Buffer

				printer.Fprint(&buf, fset, f)

				s := buf.String()

				fmt.Printf(s)
				break
			}
		}
	}
}
开发者ID:JoarLeth,项目名称:mugo,代码行数:28,代码来源:testcase_test.go


示例18: Render

// Render renders an ast node
func (scope *Scope) Render(x ast.Node) string {
	var buf bytes.Buffer
	if err := printer.Fprint(&buf, scope.fset, x); err != nil {
		panic(err)
	}
	return buf.String()
}
开发者ID:Comdex,项目名称:go-pry,代码行数:8,代码来源:interpreter.go


示例19: getSource

func getSource(t *testing.T, n ast.Node) string {
	fset := token.NewFileSet()
	var buf bytes.Buffer
	err := printer.Fprint(&buf, fset, n)
	assert.NoError(t, err)
	return buf.String()
}
开发者ID:hneemann,项目名称:yagi,代码行数:7,代码来源:generify_test.go


示例20: Process

// Process formats and adjusts returns for the provided file in a
// package in pkgDir. If pkgDir is empty, the file is treated as a
// standalone fragment (opt.Fragment should be true). If opt is nil
// the defaults are used.
func Process(pkgDir, filename string, src []byte, opt *Options) ([]byte, error) {
	if opt == nil {
		opt = &Options{}
	}

	fileSet := token.NewFileSet()
	file, adjust, typeInfo, err := parseAndCheck(fileSet, pkgDir, filename, src, opt)
	if err != nil {
		return nil, err
	}

	if err := fixReturns(fileSet, file, typeInfo); err != nil {
		return nil, err
	}

	var buf bytes.Buffer
	err = printer.Fprint(&buf, fileSet, file)
	if err != nil {
		return nil, err
	}
	out := buf.Bytes()
	if adjust != nil {
		out = adjust(src, out)
	}

	out, err = format.Source(out)
	if err != nil {
		return nil, err
	}
	return out, nil
}
开发者ID:Dereking,项目名称:GoPath,代码行数:35,代码来源:returns.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang printer.Config类代码示例发布时间:2022-05-28
下一篇:
Golang protolog.Printf函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap