Standard library / go/token
go/token.FileSet.Read()
Deserializes a file set through a decoding callback; use a compatible encoding and trusted input. Expected stdout: 'demo.go:2:1\n'.
Formal Syntax & Structure
go syntaxgo
func (s *FileSet) Read(decode func(any) error) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| decode | func(any) error | Callback that decodes serialized file-set data. |
Return Value
error
Parsing or setting error; nil on success.
Working Example
Example Snippetgo
package main
import ( "bytes" "encoding/gob" "fmt" "go/token")
func check(e error) { if e != nil { panic(e) }}
func main() { s := token.NewFileSet() f := s.AddFile("demo.go", -1, 5) f.SetLinesForContent([]byte("ab\ncd")) var b bytes.Buffer check(s.Write(gob.NewEncoder(&b).Encode)) t := token.NewFileSet() check(t.Read(gob.NewDecoder(&b).Decode)) fmt.Println(t.Position(f.Pos(3)))}