title: Golang AST - add import "C"
body: <p>I'm trying to add an <code>import "C"</code> to the AST at the begining. I want to add also some doc comment to the import node. want to look like this:</p>
  
  <pre><code>package main
  
  /*
  #include "postgres.h"
  #include "fmgr.h"
  */
  import "C"
  
  func main() {}
  </code></pre>
  
  <p>Doing this:</p>
  
  <pre><code>//parse the file
  fset := token.NewFileSet()
  f, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
  if err != nil {
      panic(err)
  }
  //get the package main
  packageAst, ok := f["main"]
  if !ok {
      fmt.Println("No package main in", dir)
      return
  }
  //add the import "C" with the comment
  for _, file := range packageAst.Files {
      comment := &amp;ast.CommentGroup{
          List: []*ast.Comment{
              &amp;ast.Comment{
                  Slash: file.Package,
                  Text:  "/*\n#include \"postgres.h\"\n#include \"fmgr.h\"\n*/",
              },
          },
      }
      importC := &amp;ast.GenDecl{
          TokPos: file.Package,
          Tok:    token.IMPORT,
          Specs:  []ast.Spec{&amp;ast.ImportSpec{Doc: comment, Path: &amp;ast.BasicLit{Kind: token.STRING, Value: "\"C\""}}},
      }
      file.Decls = append([]ast.Decl{importC}, file.Decls...)
      break
  }
  </code></pre>
  
  <p>And the output is this:</p>
  
  <pre><code>package main
  
  import "C"
  
  func main() {}
  </code></pre>
  
  <p>The comment is not there. What I'm doing wrong?</p>