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

NameTypeDescription
file*token.FileFile with a size matching the source, or file to remove from the set.
src[]byteSource input; for ParseFile, nil reads filename, otherwise string, bytes or io.Reader.
errErrorHandlerError callback for scanning, or error to print.
modeModeBit 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)}
Go Reference