August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, we are going to write a Go program that counts the number of times a keyword appears in the input files. In this case, the keyword that is going to be counted is var. The name of the utility will be varTimes.go and it is going to be presented in four parts. The first part of varTimes.go is as follows:
package main
import (
"fmt"
"go/scanner"
"go/token"
"io/ioutil"
"os"
)
var KEYWORD = "var"
var COUNT = 0
You can search for any Go keyword you want – you can even set the value of the KEYWORD global variable at runtime if you modify varTimes.go.
The second part of varTimes.go contains the following Go code:
func main() { if len(os.Args) == 1 { fmt.Println("Not enough arguments!") return } for _, file ...Read now
Unlock full access