FAQ
templates is the only sane way(AFAIK) to go right now . I've created a
library for the most commonly used constructs (e.g. package declaration,
  type declaration, struct literal declaration & assignment and some forms
of function calls) to keep the code generation manageable and
avoid redundancy but it is far from being a robust solution because the lib
was created as a workaround rather than a holistic approach based on the
language specifications. Initially I used go/ast as the IR which worked
fine for simple type declarations but it proved quite challenging on more
complex constructs so I've moved to a mix of templates (to write the source
code) and an internal representation/types.
    Recently I've moved many parts of the "generated" code into a reflect
based solution due the high cost of maintenance of the code generation
tools(i.e. my library) so I would suggest you to consider a reflect based
solution mixed with code generation(strictly only where reflect can't help)
and if the performance is an issue move from reflect only when the API is
stable. This will save you many weeks/months of work if the project is
large.

Mihai.
On Saturday, 7 May 2016 20:30:35 UTC+3, Carl Mastrangelo wrote:

In a toy project I am working on, I am trying to generate Go code
automatically to cut down on the amount of boiler plate I have to write. I
was looking at the packages under go/ in the standard library, but a lot of
them seem specific to taking existing code and interpreting it rather than
the other direction. Specifically, a lot of the API depends on having a
token.FileSet, which assumes the file already exist. Since I am making it
from scratch, I don't have a file or line number or token positions.

Is there a proper way to generate code? Looking at golang/protobuf, it
seems to print out raw strings currently too. Other projects like protobuf
would likely benefit from being able to build code using standard packages.
--
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/d/optout.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 7 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 7, '16 at 5:30p
activeMay 9, '16 at 9:34a
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase