Golang go/token.File type examples

package go/token

A File is a handle for a file belonging to a FileSet. A File has a name, size, and line offset table.

Golang go/token.File type usage examples

Example 1:

 // parse the file and in the process add it to the file set
 if ast, err = parser.ParseFile(x.fset, filename, src, parser.ParseComments); err == nil {
 file = x.fset.File(ast.Pos()) // ast.Pos() is inside the file
 return
 }

Example 2:

 var funcs = template.FuncMap{
 "filename": func(f *ast.File) string { return prog.Fset.File(f.Pos()).Name() },
 "base": filepath.Base,
 "stdpkg": isStandardPackage,
 }

Reference :

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

Advertisement