drymoeuka282427675 2011-08-13 20:41
浏览 2
已采纳

如何修改打印机。旧版本的Fprint可以在最新版本的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() + ";
"   

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?

  • 写回答

1条回答 默认 最新

  • dss89001 2011-08-13 22:18
    关注

    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() {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向