-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc.go
51 lines (42 loc) · 806 Bytes
/
wc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
func readFile(file string) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
bytes, err := ioutil.ReadAll(f)
return bytes, err
}
func wc(file string) (int,int,int,string){
var byteCount int = 0
var wordCount int = 0
var lineCount int = 0
bytes, _ := readFile(file)
byteCount = len(bytes)
var lc byte = 0
for _, b := range bytes {
if lc!=('\r') && (b == byte('\n') || b == byte('\r')){
lineCount++
}
if lc <= 0x20 && b > 0x20 {
wordCount++
}
lc = b
}
return lineCount,wordCount,byteCount, file
}
func main() {
args := os.Args
if len(args) == 1 {
fmt.Println("Please provide a file")
}
lc, wc, bc, fn := wc(args[1])
fmt.Printf("%d %d %d %s\n", lc, wc, bc, fn)
}