Goのloaderメモ
This content is a draft and will not be included in production builds.
- types.Info は PackageInfo.Info にある
- token.FileSet は Program.Fset にある
- Goで静的解析する際のテンプレート(loader版)
- Goで静的解析する際のテンプレート(標準版)
loader
Section titled “loader”loader.Config.Importはテストを読まない
Section titled “loader.Config.Importはテストを読まない”*_test.go や package x_test を対象に含めたい場合は ImportWithTests が必要。
Package.Importedはc.ImportしたPath
Section titled “Package.Importedはc.ImportしたPath”例えば
var c loader.Configc.Import("math/rand/v2")この場合、PackageInfo.Imported は math/rand/v2 だけを持つが、go/parserでパースしたファイル自身のパッケージパスは取れない。
math/rand/v2 が参照するパッケージを知りたい場合は、 PackageInfo.Pkg.Imports を調べるといい。
pkgs := c.Package("math/rand/v2").Imports()fmt.Println(pkgs[0].Name()) // Output: randfmt.Println(pkgs[0].Path()) // Output: math/rand/v2Configはコメントを消す
Section titled “Configはコメントを消す”消すので、 //go:linkname などのディレクティブも消してしまう。残したい場合はCommentsオプションを与える。
雑多
case *ast.SelectorExpr: // X.Selcase *ast.CompositeLit: // Type{}case *ast.ParenExpr: // (X)case *ast.UnaryExpr: // &Xcase *ast.IndexExpr: // X[Index]case *ast.StarExpr: // *Xcase *ast.CallExpr: // Fun()