Standard library / go/token

go/token.File.Line()

Returns the line number for a position, using adjusted coordinates. Expected stdout: '2\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Line(p Pos) int

Parameters & Attributes

NameTypeDescription
pPosCompact source position in the associated file set.

Return Value

int

Returns the line number for a position, using adjusted coordinates.

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