HN2new | past | comments | ask | show | jobs | submitlogin

C precompiler macros can be (and are) used to provide generic-like semantics.

Go has no such "blessed" precompiler-macro system.

Templates/macros are an ugly solution, but they are a solution.



The simplicity of Go's syntax makes it really easy to use as a target for compile-time generated code, and all of the built-in parsing libraries in the standard lib make it really easy to interface such generated code with human-generated code and at the end of all of this you end up with something much more type-safe than you do with C macros.

So yeah, the toolset may not have anything like the C's preprocessor (the lack of which is a blessing, not a curse, IMO), but that's fine, if you're sure you really need such a thing just spend half a day writing your own in Go that is specific to your project's needs and make that a part of your local build process.


Are there any good examples of this kind of approach? Any tooling support for this?


None that is publicly available that I'm aware of, unfortunately.

No real tooling either other than whatever you choose to use for the build process. For the projects I've done this for that ends up being a very simple Makefile which does a 'go run' on the code generation engine code prior to a 'go build' of the resulting combined package of generated & hand-written code.


Do you use some custom code generation engine or gen[1]?

[1] http://clipperhouse.github.io/gen/ ("A library for bringing generics-like functionality to Go")


Custom -- most of the code generation I've done has been about binding Go functionality into UI markup languages (similar to the way ActionScript binding in Flex works, for those who have used that) as opposed to generics-like functionality.

I've never used gen, it looks pretty nice though, at least based on the linked docs and it is probably a good answer to Pxtl's question about publicly available examples.


> macros are an ugly solution, but they are a solution

Tangential, but a decent example of that sort of thing, a generic red-black tree written entirely in the C preprocessor: http://www.canonware.com/download/rb/rb_newer/rb.h





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: