FAQ
Hi,

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.

Search Discussions

  • Josh Bleecher Snyder at Oct 10, 2013 at 3:33 pm
    Is there an idiomatic way for Go packages to generate Go source code?
    Use text/template and then gofmt.

    I could not find any in the Go standard library.
    http://golang.org/src/cmd/go/test.go

    See also Andrew and André's comments in

    https://groups.google.com/forum/#!searchin/golang-nuts/advice$20needed$3A$20code$20generation/golang-nuts/EweaZMT7TZI/X_NRYFzclpsJ

    (Man I wish Google Groups would put a link to the thread on www in each email.)

    -josh

    --
    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.
  • Tom Payne at Oct 10, 2013 at 4:30 pm
    Many thanks Josh!

    On 10 October 2013 17:32, Josh Bleecher Snyder wrote:

    Is there an idiomatic way for Go packages to generate Go source code?
    Use text/template and then gofmt.

    I could not find any in the Go standard library.
    http://golang.org/src/cmd/go/test.go

    See also Andrew and André's comments in


    https://groups.google.com/forum/#!searchin/golang-nuts/advice$20needed$3A$20code$20generation/golang-nuts/EweaZMT7TZI/X_NRYFzclpsJ

    (Man I wish Google Groups would put a link to the thread on www in each
    email.)

    -josh

    --
    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.
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 10, '13 at 2:05p
activeOct 10, '13 at 4:30p
posts3
users2
websitegolang.org

2 users in discussion

Tom Payne: 2 posts Josh Bleecher Snyder: 1 post

People

Translate

site design / logo © 2022 Grokbase