Skip to content

Commit

Permalink
convert sample to go
Browse files Browse the repository at this point in the history
  • Loading branch information
jehiah committed Dec 19, 2014
1 parent 7a761b4 commit 9784f77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
build
sample/sample
37 changes: 37 additions & 0 deletions sample/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)

func main() {

if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)")
os.Exit(1)
}

target, err := strconv.ParseFloat(os.Args[1], 64)
if err != nil || target < 0.0 || target > 1.0 {
fmt.Fprintf(os.Stderr, "Unable to convert %q to a float between 0.0 and 1.0", os.Args[1])
os.Exit(1)
}

rand.Seed(time.Now().UnixNano())
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if target < rand.Float64() {
continue
}
fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n'
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
os.Exit(2)
}
}

0 comments on commit 9784f77

Please sign in to comment.