Skip to content

Commit eb0e11c

Browse files
committed
convert sample to go
1 parent 289fe27 commit eb0e11c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

sample/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sample

sample/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"math/rand"
7+
"os"
8+
"strconv"
9+
"time"
10+
)
11+
12+
func main() {
13+
14+
if len(os.Args) != 2 {
15+
fmt.Fprintf(os.Stderr, "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)")
16+
os.Exit(1)
17+
}
18+
19+
target, err := strconv.ParseFloat(os.Args[1], 64)
20+
if err != nil || target < 0.0 || target > 1.0 {
21+
fmt.Fprintf(os.Stderr, "Unable to convert %q to a float between 0.0 and 1.0", os.Args[1])
22+
os.Exit(1)
23+
}
24+
25+
rand.Seed(time.Now().UnixNano())
26+
scanner := bufio.NewScanner(os.Stdin)
27+
for scanner.Scan() {
28+
if target < rand.Float64() {
29+
continue
30+
}
31+
fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n'
32+
}
33+
if err := scanner.Err(); err != nil {
34+
fmt.Fprintln(os.Stderr, "reading standard input:", err)
35+
os.Exit(2)
36+
}
37+
}

0 commit comments

Comments
 (0)