forked from uia-worker/pprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcount01.go
46 lines (38 loc) · 1.07 KB
/
wordcount01.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
package main
import (
"fmt"
"io"
"log"
"os"
"unicode"
"github.com/pkg/profile"
)
func readbyte(r io.Reader) (rune, error) {
var buf [1]byte
_, err := r.Read(buf[:])
return rune(buf[0]), err
}
func main() {
// For å generee profiler; kun en aktivert av gangen
//defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
//defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
defer profile.Start(profile.MemProfile, profile.MemProfileRate(1), profile.ProfilePath(".")).Stop()
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("Kunne ikke åpne filen %q: %v", os.Args[1], err)
}
words := 0
inword := false // tilstandsmaskin at vi er inn et ord (obs! unicode)
for {
r, err := readbyte(f)
if err == io.EOF {
break
}
if unicode.IsSpace(r) && inword {
words++
inword = false
}
inword = unicode.IsLetter(r)
}
fmt.Printf("%q: %d words\n", os.Args[1], words)
}