I have a few different code gen projects and I generally use
text/template but don't worry about formatting/indentation, just
getting valid output. Then I run the result through go/format as Jan
suggested (which is the library equivalent of gofmt).
> One of the reasons I was looking at go/ast in the first place is that it is hard to tell if an import is used or not until the whole source is generated.
For generated code, I think it's reasonable not to worry about this.
You can just add dummy usage so the code compiles.
import (
"bufio"
"fmt"
"strconv"
)
var (
_ = bufio.Scanner
_ = fmt.Sprint
_ = strconv.Atoi
)
This isn't acceptable in hand-written code, but as r and others like
to say, we hold generated code to a lower standard. (Protobuf and lots
of other projects take this approach.)
-Caleb
> --
> 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...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.