Original post

Dear All,
I am doing AST based transformation in . I am trying to add an extra import package “manipulate”, and set it to the existing ones. Such that the input and output look like the following:

INPUT PROGRAM
package main
import (
. “a”
)
**** OUTPUT PROGRAM ****
package main
import (
. “a”
. “manipulate”
)

In the following code. I get the wrong output like:
package main
import (
. “a”
)
import (
“manipulate”
)

Any help on how to solve the above problem ?

Here is the snippet code:
package main

import (
“fmt”
“go/ast”
“go/parser”
“go/printer”
“go/token”
“log”
“os”
)

func exercises1(filename string) {
// Create the AST by parsing the source code.
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}

bascilit := &ast.BasicLit{Kind: token.STRING, Value: ""manipulate""} 
identdot := ast.NewIdent(" ")
importspec := &ast.ImportSpec{Name: identdot, Path: bascilit}
import0 := &ast.GenDecl{Specs: []ast.Spec{importspec}, Tok: token.IMPORT, Lparen: 3} 

for _, f := range node.Decls {
        _, ok := f.(*ast.GenDecl)
        if ok {
                list := append(node.Decls, import0)
                node.Decls = list
        }
}

}
func main() {
exercises1(“exercise1.go”)
}