Golang go/token.File.Position function examples

package go/token

Position returns the Position value for the given file position p(1st parameter); p must be a Pos value in that file or NoPos.

Golang go/token.File.Position function usage examples

Example 1:

 fs := token.NewFileSet()
 tf := fs.AddFile("", fs.Base(), len(data))
 tf.SetLinesForContent(data)
 pos := tf.Position(tf.Pos(offset))
 return pos.Line, pos.Column

Example 2:

 // verify scan
 var S Scanner
 file := fset.AddFile(filepath.Join("dir", "TestLineComments"), fset.Base(), len(src))
 S.Init(file, []byte(src), nil, dontInsertSemis)
 for _, s := range segs {
 p, _, lit := S.Scan()
 pos := file.Position(p)
 checkPos(t, lit, p, token.Position{
 Filename: s.filename,
 Offset: pos.Offset,
 Line: s.line,
 Column: pos.Column,
 })
 }

Reference :

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

Advertisement