-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (54 loc) · 1.28 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
package main
import (
"flag"
"fmt"
"os"
"strings"
"unicode/utf8"
)
func main() {
cFlag := flag.Bool("c", false, "Used for counting number of bytes in the file")
lFlag := flag.Bool("l", false, "Used for counting number of lines in the file")
wFlag := flag.Bool("w", false, "Used for counting number of words in the file")
mFlag := flag.Bool("m", false, "Used for counting Characters in the file")
flag.Parse()
path := flag.Arg(0)
file, err := os.ReadFile(path)
if err != nil {
fmt.Println("Please pass file path")
os.Exit(0)
}
var out any
switch {
case *cFlag:
out = calculateBytes(file)
case *lFlag:
out = calculateLines(file)
case *wFlag:
out = calculateWords(file)
case *mFlag:
out = calculateCharacters(file)
default:
out = fmt.Sprintf("%d\t%d\t%d\t%d", calculateBytes(file), calculateLines(file), calculateWords(file), calculateCharacters(file))
}
fmt.Printf("%v %s\n", out, path)
}
func calculateBytes(file []byte) int {
//bytesSize := binary.Size(file)
return len(file)
}
func calculateLines(file []byte) int {
lines := 0
for _, ch := range file {
if ch == '\n' {
lines++
}
}
return lines
}
func calculateWords(file []byte) int {
return len(strings.Fields(string(file)))
}
func calculateCharacters(file []byte) int {
return utf8.RuneCount(file)
}