1. The Go gopher was designed by Renée French.
The gopher stickers was made by Takuya Ueda.
Licensed under the Creative Commons 3.0 Attributions license.
エディタの壁を超える
Goの開発ツールの
文化と作成法
2016/11/05(土)
@VimConf 2016
23. 式の構文解析
n, _ := parser.ParseExpr(`v + 1`)
ast.Inspect(n, func(n ast.Node) bool {
if n != nil { fmt.Printf("%Tn", n) }
return true
})
printer.Fprint(os.Stdout, token.NewFileSet(), n)
*ast.BinaryExpr
*ast.Ident
*ast.BasicLit
v + 1
+
v 1
構文解析
抽象構文木(AST)を探索
抽象構文木(AST)を出力
BinaryExpr
Ident BasicLit
Playgroundで動かす
24. コメントの抽出
const src = `package main
func main() {v := 100 /*comment for v*/}`
fs := token.NewFileSet()
f, _ := parser.ParseFile(fs,"my.go",src, parser.ParseComments)
cmap := ast.NewCommentMap(fs, f, f.Comments)
for n, cgs := range cmap {
printer.Fprint(os.Stdout, fs, n)
fmt.Println()
for _, cg := range cgs { fmt.Println(cg.Text()) }
}
v := 100
comment for v
Playgroundで動かす
構文解析
コメントと
ノードの対応を取得
25. 宣言場所の取得
const src = `package main
var v = 100
func main() { fmt.Println(v+1) }`
fs := token.NewFileSet()
f, _ := parser.ParseFile(fs, "my.go", src, 0)
i := &types.Info{Defs: map[*ast.Ident]types.Object{}}
(&types.Config{}).Check("main", fs, []*ast.File{f}, i)
for n, o := range i.Defs { fmt.Println(n, o) }
main <nil>
v var main.v int
main func main.main()
Playgroundで動かす 宣言の取得
Usesを使えば
使用箇所も調べられる
29. ライブラリとして提供する利点
■ 他のツール内で利用しやすい
● gorenameを利用したツールを作る場合
type User struct {
Id int64
Name string
}
type User struct {
ID int64 `datastore:"Id"`
Name string `datastore:"Name"`
}
フィールド名をリファクタリングしたいけど、
データストア上のカラム名は変えたくない!
gorenameを自作ツールから呼び出して
リネームしつつstructタグを付けていくイチから作る必要がない!