Created
June 24, 2017 05:13
-
-
Save tenntenn/706d73e0d82105b0d25179578b953745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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