Skip to content

Commit 12a0f28

Browse files
committed
count output
1 parent eb0e11c commit 12a0f28

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sample/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
func main() {
13-
13+
var total int64
14+
var emitted int64
1415
if len(os.Args) != 2 {
1516
fmt.Fprintf(os.Stderr, "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)")
1617
os.Exit(1)
@@ -25,13 +26,17 @@ func main() {
2526
rand.Seed(time.Now().UnixNano())
2627
scanner := bufio.NewScanner(os.Stdin)
2728
for scanner.Scan() {
29+
total += 1
2830
if target < rand.Float64() {
2931
continue
3032
}
33+
emitted += 1
3134
fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n'
3235
}
3336
if err := scanner.Err(); err != nil {
34-
fmt.Fprintln(os.Stderr, "reading standard input:", err)
37+
fmt.Fprintln(os.Stderr, "Error reading standard input:", err)
3538
os.Exit(2)
3639
}
40+
41+
fmt.Fprintf(os.Stderr, "Total of %d lines. Sampled to %d\n", total, emitted)
3742
}

0 commit comments

Comments
 (0)