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
+13.8k Golang : Gin framework accept query string by post request example
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+5.6k Golang : Detect words using using consecutive letters in a given string
+6.9k Android Studio : Hello World example
+12.3k Golang : Print UTF-8 fonts on image example
+15.3k nginx: [emerg] unknown directive "ssl"
+17.7k Golang : Read data from config file and assign to variables
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+13.6k Golang : Query string with space symbol %20 in between
+21.2k Golang : Get password from console input without echo or masked
+15k Golang : package is not in GOROOT during compilation