Standard library / go/token

go/token.File.Pos()

Converts a byte offset to a compact position, clamping outside bounds. Expected stdout: '4 1 6\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Pos(offset int) Pos

Parameters & Attributes

NameTypeDescription
offsetintZero-based byte offset within the source file.

Return Value

Pos

Converts a byte offset to a compact position, clamping outside bounds.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/token")
func main() {  s := token.NewFileSet()  f := s.AddFile("demo.go", -1, 5)  f.SetLinesForContent([]byte("ab\ncd"))  fmt.Println(f.Pos(3), f.Pos(-1), f.Pos(99))}
Go Reference