As Ian Lance Taylor answered already, Go language and main Go compilers (go and gccgo) do not support this.
My unofficial Go interpreter https://github.com/cosmos72/gomacro instead does, I even presented its AST manipulation and code generation facilities (heavily modeled after Common Lisp) at golab.io this October. If you know Common Lisp syntax for quoting and backquoting/quasiquoting, the introduction of https://github.com/cosmos72/gomacro/blob/master/doc/quasiquote.md shows how to write the same in gomacro. Example: gomacro> add := ~'{1+2} gomacro> add 1 + 2 // *go/ast.BinaryExpr gomacro> add.X 1 // go/ast.Expr gomacro> add.Y 1 // go/ast.Expr gomacro> add.Op + // go/token.Token gomacro> :inspect add add = 1 + 2 // *ast.BinaryExpr 0. X = {ValuePos:20 Kind:INT Value:1} // ast.Expr 1. OpPos = 21 // token.Pos 2. Op = + // token.Token 3. Y = {ValuePos:22 Kind:INT Value:2} // ast.Expr // type ? for inspector help inspect add> q gomacro> On Friday, December 14, 2018 at 6:25:30 AM UTC+1, Dmitry Ponyatov wrote: > > Is it possible to do metaprogramming in Go? > > What I want to have is a way to run arbitrary code in compile time, which > has full access to compiler data structures > (parsed syntax trees, compiler stages callbacks, direct code generator > calls). > > As a variant, it can be some Lisp system runs between parser stage and the > rest of Go compiler. > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.