Golang go/token.FileSet.File function examples

package go/token

File returns the file that contains the position p(1st parameter). If no such file is found (for instance for p == NoPos), the result is nil.

Golang go/token.FileSet.File function usage examples

Example 1:

 func FindFileAst(fset *token.FileSet, file *token.File, fileAsts []*ast.File) *ast.File {
 for _, fileAst := range fileAsts {
 if fset.File(fileAst.Package) == file {
 return fileAst
 }
 }
 return nil
 }

Example 2:

 for file, pos := range offsets {
 f := fset.File(Pos(pos))
  if f.Name() != file {
 t.Errorf("got %q at position %d, want %q", f.Name(), pos, file)
  }
 }

References :

http://golang.org/src/pkg/go/token/position_test.go

http://golang.org/pkg/go/token/#FileSet.File

Advertisement