title: Using the go/ast package
body: <p>I'm attempting to do some metaprogramming in Go and I'm trying to use the <code>go/ast</code> package.</p>
  
  <p>Here's the snippet which is creating the AST:</p>
  
  <pre><code>// Create an AST
  fooFn := &amp;ast.FuncDecl{
      Name: &amp;ast.Ident{Name: "foofn"},
      Body: &amp;ast.BlockStmt{
          List: []ast.Stmt{&amp;ast.ExprStmt{
              X: &amp;ast.CallExpr{
                  Fun: &amp;ast.Ident{Name: "println"},
                  Args: []ast.Expr{
                      &amp;ast.BasicLit{Kind: token.INT, Value: "42"},
                  },
              },
          }},
      },
  }
  
  f := &amp;ast.File{
      Name:  &amp;ast.Ident{Name: "foopackage"},
      Decls: []ast.Decl{fooFn},
      Scope: &amp;ast.Scope{
          Objects: map[string]*ast.Object{
              "Foo": &amp;ast.Object{
                  Kind: ast.Fun,
                  Name: "Foo",
                  Decl: fooFn,
              },
          },
      },
  }
  </code></pre>
  
  <p>See the full code here <a href="http://play.golang.org/p/zTJdnqVx__">http://play.golang.org/p/zTJdnqVx__</a> which should produce this output:</p>
  
  <pre><code>panic: runtime error: invalid memory address or nil pointer dereference
  [signal 0xb code=0xffffffff addr=0x0 pc=0x773ae]
  
  goroutine 16 [running]:
  runtime.panic(0x18b5a0, 0x24c8af)
      /tmp/sandbox/go/src/pkg/runtime/panic.c:279 +0x120
  go/ast.(*FuncType).Pos(0x0, 0x1c827eb4, 0xfefd02b8, 0x10422360)
      /tmp/sandbox/go/src/pkg/go/ast/ast.go:446 +0x2e
  go/ast.(*FuncDecl).Pos(0x10422360, 0x10422360, 0x47, 0xfeee1cb0)
      /tmp/sandbox/go/src/pkg/go/ast/ast.go:933 +0x40
  go/printer.(*printer).declList(0x10402840, 0x1040e108, 0x1, 0x1)
      /tmp/sandbox/go/src/pkg/go/printer/nodes.go:1587 +0x180
  go/printer.(*printer).file(0x10402840, 0x10442080)
      /tmp/sandbox/go/src/pkg/go/printer/nodes.go:1597 +0x1e0
  go/printer.(*printer).printNode(0x10402840, 0x184ba0, 0x10442080, 0x104223c0, 0x0, 0x0)
      /tmp/sandbox/go/src/pkg/go/printer/printer.go:1087 +0xa40
  go/printer.(*Config).fprint(0xfeee1e90, 0xfefd02d8, 0x10426180, 0x104181b0, 0x184ba0, 0x10442080, 0x104223c0, 0xfefd02d8, 0x0, 0x0)
      /tmp/sandbox/go/src/pkg/go/printer/printer.go:1226 +0xc0
  go/printer.(*Config).Fprint(0xfeee1e90, 0xfefd02d8, 0x10426180, 0x104181b0, 0x184ba0, 0x10442080, 0x0, 0x0)
      /tmp/sandbox/go/src/pkg/go/printer/printer.go:1284 +0xa0
  main.main()
      /tmpfs/gosandbox-eb231fa6_4e4e43ec_79a6f993_ce4170ce_07ee185b/prog.go:46 +0x8c0
  </code></pre>
  
  <hr>
  
  <p>Edit: here is the working version with <code>FuncDelc.Type</code> set and <code>Scope</code> removed (since I'm only after the source code buffer):</p>
  
  <h3><a href="http://play.golang.org/p/B0Xp61rSce">http://play.golang.org/p/B0Xp61rSce</a></h3>