Standard library / go/token

go/token.File.Position()

Returns the adjusted filename, byte offset, line and byte column for a position. Expected stdout: 'demo.go:2:2\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Position(p Pos) (pos Position)

Parameters & Attributes

NameTypeDescription
pPosCompact source position in the associated file set.

Return Value

(pos Position)

Returns the adjusted filename, byte offset, line and byte column for a position.

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.Position(f.Pos(4)))}
Go Reference