开发者

How to modify printer.Fprint in old version to can run on latest version of Go

str := new(bytes.Buffer) //old code
printer.Fprint(str, c)   //old code 
str := new(token.FileSet) //new code
printer.Fprint(os.Stdout, str, c) //new code    

source += "\t" + str.String() + ";\n"   

In this code i try to change str's value from new(bytes.Buffer) to new(token.FileSet) 开发者_高级运维because Fprint's argument requier;

func Fprint(output io.Writer, fset *token.FileSet, node interface{}) os.Error //latest ver.

now, i'm stucking in error str.String() because str don't have method String(). I cann't update my code for run in latest version of Go because a changed of printer.Fprint()

How to volve this?


Here's a sample program.

package main

import (
    "bytes"
    "fmt"
    "go/parser"
    "go/printer"
    "go/token"
)

func main() {
    const src = `package main
    func main() {}
    `

    fset := token.NewFileSet()
    ast, err := parser.ParseFile(fset, "", src, parser.ParseComments)
    if err != nil {
        panic(err)
    }

    var buf bytes.Buffer
    printer.Fprint(&buf, fset, ast)

    fmt.Print(buf.String())
}

Output:

package main

func main() {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜