Short version: is there an idiomatic way for Go packages to generate Go
source code? I could not find any in the Go standard library.
Long version:
I'm writing Go code to represent OGC-style geometries (for geospatial
applications). Much of the code is repetitive. For example, there are four
types of point:
Point (x and y coordinates only)
PointZ (x, y, and z)
PointM (x, y, and some measure m, e.g. time)
PointZM (x, y, z, and m)
Although its possible to use reflection to handle these cases, given that
the types are very simple and widely used, reflection feels like overkill
here. The code can easily be generated with templates.
Right now I have a simple top-level generate.go script:
https://github.com/twpayne/gogeom/blob/master/generate.go
which parses and executes templates like:
https://github.com/twpayne/gogeom/blob/master/geom/point.go.tmpl
to generate correctly-formatted Go code:
https://github.com/twpayne/gogeom/blob/master/geom/point.go
The output files are checked into repository so users do not have run the
generator.
So, the specific questions are:
- Can generating Go code with text/template ever be considered idiomatic
Go? Or should reflection always be used?
- If generation with text/template is OK, is there an idiomatic structure
to follow? e.g. Where should the generator script and templates be stored
in the package? Should a Makefile be provided?
Many thanks,
Tom
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.