example3.go 783 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // blame: jnml, labs.nic.cz
  5. // +build ignore
  6. package main
  7. import (
  8. "bufio"
  9. "flag"
  10. "log"
  11. "math/rand"
  12. "os"
  13. )
  14. /*
  15. $ # Usage e.g.:
  16. $ go run example3.go -max 1024 > rand.dat # generate 1kB of "random" data
  17. */
  18. func main() {
  19. r := rand.New(rand.NewSource(1))
  20. var mflag uint64
  21. flag.Uint64Var(&mflag, "max", 0, "limit output to max bytes")
  22. flag.Parse()
  23. stdout := bufio.NewWriter(os.Stdout)
  24. if mflag != 0 {
  25. for i := uint64(0); i < mflag; i++ {
  26. if err := stdout.WriteByte(byte(r.Int())); err != nil {
  27. log.Fatal(err)
  28. }
  29. }
  30. stdout.Flush()
  31. return
  32. }
  33. for stdout.WriteByte(byte(r.Int())) == nil {
  34. }
  35. }