Glad you got things working. A few quick remarks:

1) You don't need to call ParseDir or CreateFromFiles. The loader will 
figure out what files you need to load for each package; just call 
config.Import(packagename).

2) The go/types tutorial says that export data doesn't contain position 
information. That used to be true, but we have since extended the export 
data format to include it.  I'll update it.

3) That said, I did once notice a bug whereby the position of the package 
declaration was reported instead of the correct location. If you are able 
to reproduce that bug, please report it.

cheers
alan

 

> On Sun, Oct 29, 2017 at 5:00 PM, Keynan Pratt <kpr...@atlassian.com 
> <javascript:>> wrote:
>
>> Can confirm the following works as expected:
>>
>>
>> package main
>>
>> import (
>>     "fmt"
>>     "go/parser"
>>     "go/token"
>>     "go/ast"
>>     sourceLoader "golang.org/x/tools/go/loader"
>> )
>>
>> func main() {
>>     pkgPath := "bitbucket.org/.../test_package"
>>     dir := "../..//test_package"
>>
>>     fset := token.NewFileSet()
>>     packages, err := parser.ParseDir(fset, dir, nil, parser.AllErrors)
>>     if err != nil {
>>         fmt.Println(err)
>>         return
>>     }
>>
>>     for _, p := range packages {
>>         files := make([]*ast.File, 0, 8)
>>         for _, file := range p.Files {
>>             files = append(files, file)
>>         }
>>
>>         loader := sourceLoader.Config{Fset: fset}
>>         loader.CreateFromFiles(pkgPath, files...)
>>         prgm, err := loader.Load()
>>         if err != nil {
>>             fmt.Errorf(err.Error())
>>         }
>>
>>         info := prgm.Created[0].Info
>>         fmt.Println("\tUsages from ", pkgPath)
>>         for use := range info.Uses {
>>             obj := info.Uses[use]
>>
>>             pkgOfUse := obj.Pkg()
>>             nameOfUse := obj.Name()
>>             declPos := prgm.Fset.Position(obj.Pos())
>>             if pkgOfUse == nil {
>>
>>                 fmt.Println("Package was nil for obj.id [ ", obj.Id(), " ]")
>>                 continue
>>             }
>>             fmt.Printf(
>>                 "\tUsage of %s # %s declared at %s:%d:%d\n",
>>                 pkgOfUse.Path(),
>>                 nameOfUse,
>>                 declPos.Filename, declPos.Line, declPos.Column)
>>         }
>>     }
>> }
>>
>>
 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to