-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (37 loc) · 765 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
)
func main() {
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "template/template.go", nil, 0)
if err != nil {
fmt.Println("Can't parse file", err)
}
ast.Print(nil, file)
ast.Walk(MyVisit(Visit), file)
f, err := os.Create("out/main.go")
if err != nil {
fmt.Println("Can't open new file")
}
defer f.Close()
printer.Fprint(f, fs, file)
}
type MyVisit func(node ast.Node) ast.Visitor
func (f MyVisit) Visit(node ast.Node) (w ast.Visitor) {
return f(node)
}
func Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.CallExpr:
if v, ok := n.Args[0].(*ast.BasicLit); ok {
v.Value = "\"Fuga\""
}
}
return MyVisit(Visit)
}