I have a set of XML description of network protocol that I'm trying to generate Go code from, therefore I don't have any existing Go code to work with. All of the examples that make use of go/ast
(such as go fmt
) take existing code, do some transformation on the AST, then write them back out. Since all I have are XML files, the AST that I'm working with have to be written by hand. The problem is that I cannot get my hand-written AST to output.
package main
import (
"go/ast"
"go/printer"
"go/token"
"os"
)
func main() {
f := ast.File{
Name: ast.NewIdent("foo"),
Decls: []ast.Decl{
&ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{
&ast.TypeSpec{
Name: ast.NewIdent("Bar"),
Type: ast.NewIdent("uint32"),
},
},
},
},
}
fset := token.NewFileSet()
printer.Fprint(os.Stdout, fset, f)
}
Expected output:
package foo
type Bar uint32
Actual output: nothing
How do I get the AST to print?