n...@mit.edu writes:

> As can be seen from my recent posts, I've been playing a lot with the 
> automatic generation if go programs using go/ast and related packages.
>
> My current problem is that when I use astutil.AddImport.
>
> When I rn the program below, which creates a new ast.File and uses 
> astutil.AddImport to import to packages into it, the serialization of that 
> File only includes the first import:
>
> go run add_import.go
> package foo
>
> import "fmt"
>
> Has anyone else seen this?
>

Arguably it's a bug in AddImport.  You need a valid Lparen for it to
print multiple specs. Try this after you add call AddImport:

        newAstFile.Decls[0].(*ast.GenDecl).Lparen = token.Pos(1)


go/ast is nice, but I'm in the middle of using it to generate a large
amount of code and am looking at just copying go/ast and making some
changes.

 - token.Pos and filesets just aren't convenient to use for code
   generation
 - Walk/Format can't handle non go/ast nodes.  I want to insert some
   temporary nodes that I can replace later on once I determine what
   type they should really be.
 - oddities like you ran into, you'll see the same thing when creating a
   type alias (set Assign to something besides token.NoPos for an alias)

- Todd

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to