inital commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.exe
|
||||
38
main.go
Normal file
38
main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Println("need to provide filename!")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
file, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
var wordCount int
|
||||
for scanner.Scan() {
|
||||
// This is done for each line of the file.
|
||||
words := strings.Fields(scanner.Text())
|
||||
wordCount += len(words)
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
log.Println(scanner.Err())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Found %d words", wordCount)
|
||||
}
|
||||
Reference in New Issue
Block a user