Standard library / go/token

go/token.FileSet.PositionFor()

Converts a position with optional line-directive adjustment. Expected stdout: 'demo.go:2:1 virtual.go:9:1\n'.

Formal Syntax & Structure

go syntaxgo
func (s *FileSet) PositionFor(p Pos, adjusted bool) (pos Position)

Parameters & Attributes

NameTypeDescription
pPosCompact source position in the associated file set.
adjustedboolWhether to honor position-altering line directives.

Return Value

(pos Position)

Converts a position with optional line-directive adjustment.

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"))  f.AddLineInfo(3, "virtual.go", 9)  fmt.Println(s.PositionFor(f.Pos(3), false), s.PositionFor(f.Pos(3), true))}
Go Reference