Golang go/scanner.Scanner.Scan() function examples
package go/scanner
Scan scans the next token and returns the token position, the token, and its literal string if applicable. The source end is indicated by token.EOF.
Golang go/scanner.Scanner.Scan() function usage examples
Example 1:
var s scanner.Scanner
pos, tok, lit = s.Scan()
if tok != token.IDENT || (lit != "true" && lit != "false") {
fmt.Println("expected boolean value")
}
Example 2: (from http://golang.org/pkg/go/scanner/#Scanner.Scan )
package main
import (
"fmt"
"go/scanner"
"go/token"
)
func main() {
// src is the input that we want to tokenize.
src := []byte("cos(x) + 1i*sin(x) // Euler")
// Initialize the scanner.
var s scanner.Scanner
fset := token.NewFileSet() // positions are relative to fset
file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
// Repeated calls to Scan yield the token sequence found in the input.
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break
}
fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+8.1k Golang : Append and add item in slice
+4.9k HTTP common errors and their meaning explained
+10.6k Golang : ISO8601 Duration Parser example
+40.5k Golang : Convert to io.ReadSeeker type
+33.8k Golang : convert(cast) bytes to string
+19k Golang : Padding data for encryption and un-padding data for decryption
+7.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+7k Golang : How to call function inside template with template.FuncMap
+8.8k Golang : Get final balance from bit coin address example
+7.1k Golang : Gorrila mux.Vars() function example
+10.8k Golang : Natural string sorting example