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.
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:
which parses and executes templates like:
to generate correctly-formatted Go code:
The output files are checked into repository so users do not have run the
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?
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 email@example.com.
For more options, visit https://groups.google.com/groups/opt_out.