2011-08-13 81 views
0
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" 

的在這段代碼我嘗試新的(bytes.Buffer)改變STR的價值,以新(token.FileSet),因爲Fprint的說法requier ;
func Fprint(output io.Writer, fset *token.FileSet, node interface{}) os.Error //latest ver.
現在,我在錯誤str.String()因爲str沒有方法String()。 我無法更新我的代碼以便在最新版本的Go中運行,因爲更改了printer.Fprint()
如何解決此問題?如何修改printer.Fprint在舊版本上都可以運行最新版本,請

回答

1

下面是一個示例程序。

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()) 
} 

輸出:

package main 

func main() {} 
相關問題