Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Created June 24, 2017 05:13
Show Gist options
  • Save tenntenn/706d73e0d82105b0d25179578b953745 to your computer and use it in GitHub Desktop.
Save tenntenn/706d73e0d82105b0d25179578b953745 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
)
func main() {
const src = `package main
import "fmt"
func main() {
n := 10
fmt.Println(n)
}`
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "main.go", src, 0)
if err != nil {
log.Fatal(err)
}
config := &types.Config{
Importer: importer.Default(),
}
info := &types.Info{
Defs: map[*ast.Ident]types.Object{},
}
_, err = config.Check("main", fs, []*ast.File{f}, info)
if err != nil {
log.Fatal(err)
}
it := types.Universe.Lookup("int").Type()
for idnt, o := range info.Defs {
if o != nil &&
types.Identical(o.Type(), it) {
fmt.Println(fs.Position(idnt.Pos()), idnt)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment