Standard library / go/scanner
go/scanner.Scanner.Init()
Initializes scanner state with a file of matching byte size; mismatch panics. May invoke error callback immediately. Expected stdout: 'IDENT x\n'.
Formal Syntax & Structure
go syntaxgo
func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| file | *token.File | File with a size matching the source, or file to remove from the set. |
| src | []byte | Source input; for ParseFile, nil reads filename, otherwise string, bytes or io.Reader. |
| err | ErrorHandler | Error callback for scanning, or error to print. |
| mode | Mode | Bit flags controlling the scanner or parser. |
Working Example
Example Snippetgo
package main
import ( "fmt" "go/scanner" "go/token")
func main() { src := []byte("x := 1") f := token.NewFileSet().AddFile("demo.go", -1, len(src)) var s scanner.Scanner s.Init(f, src, nil, 0) _, t, l := s.Scan() fmt.Println(t, l)}