Golang go/token.File.Pos function examples

package go/token

Pos returns the Pos value for the given file offset; the offset must be <= f.Size(). f.Pos(f.Offset(p)) == p.

Golang go/token.File.Pos function usage examples

Example 1:

 if 0 <= startOffset && startOffset <= file.Size() {
 start = file.Pos(int(startOffset))
 } else {
 err = fmt.Errorf("start position is beyond end of file")
 return
 }

Example 2:

 func getPos(filename string, offset int) token.Pos {
 if f := getFile(filename); f != nil {
 return f.Pos(offset)
 }
 return token.NoPos
 }

Reference :

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

Advertisement