4 ms·
Having written a data serialization library based on codegen, I would have to say that Go is relatively simple to codegen. One big annoyance is the import syst
by Vendan 8y ago
Having written a data serialization library based on codegen, I would have to say that Go is relatively simple to codegen. One big annoyance is the import system of "if you don't need it, it's an error to import it.", which, while simple enough for manual code, means you have to do extra work while generating code to keep track of what you actually use (i.e. if none of the things are a time.Time, you don't need to import "time"). The other approach is to use stuff like "var _ = time.Now" kind of constructs, which do make the codegen easier, but I personally feel are far dirtier.
One very nice feature of go is that the formatter is available as a library (https://golang.org/pkg/go/format/#Source https://golang.org/pkg/go/format/#Source), so you can gen "raw" code and then throw it through the formatter to get nice pretty output.
- dullgiulio 8y agoUnused imports are definitely not an unsolved problem: as last step after dumbly generating code, run goimports.
- Vendan 8y agoNever said "unsolved", I said "annoyance". And I tend to write the code to figure it out and do the right thing. My annoyance is with code gens that do https://github.com/golang/protobuf/blob/master/protoc-gen-go/generator/generator.go#L1340-L1343 https://github.com/golang/protobuf/blob/master/protoc-gen-go.... Not even goimports will strip those...