5
Tôi đang tìm cách tạo mã nguồn Go.Tạo mã nguồn Go
Tôi tìm thấy go/parser để tạo một biểu mẫu AST một tệp nguồn Go nhưng không thể tìm thấy cách tạo nguồn Go từ AST.
Tôi đang tìm cách tạo mã nguồn Go.Tạo mã nguồn Go
Tôi tìm thấy go/parser để tạo một biểu mẫu AST một tệp nguồn Go nhưng không thể tìm thấy cách tạo nguồn Go từ AST.
Để chuyển đổi AST sang dạng nguồn, người dùng có thể sử dụng gói go/printer.
Ví dụ (hình thức thích nghi của example khác)
package main
import (
"go/parser"
"go/printer"
"go/token"
"os"
)
func main() {
// src is the input for which we want to print the AST.
src := `
package main
func main() {
println("Hello, World!")
}
`
// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
printer.Fprint(os.Stdout, fset, f)
}
(cũng here)
Output:
package main
func main() {
println("Hello, World!")
}
Cảm ơn bạn! Rất hữu ích. –