Learning the ins and outs of the bufio library...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
805 B

9 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "os"
  5. "strings"
  6. )
  7. // pipedInput looks for data queued up on stdin.
  8. func pipedInput() bool {
  9. stat, _ := os.Stdin.Stat()
  10. if (stat.Mode() & os.ModeCharDevice) == 0 {
  11. return true
  12. }
  13. return false
  14. }
  15. func main() {
  16. // Process input and output with bufio
  17. sti := bufio.NewScanner(os.Stdin)
  18. sto := bufio.NewWriter(os.Stdout)
  19. ste := bufio.NewWriter(os.Stderr)
  20. // Check for existing data
  21. if !pipedInput() {
  22. // Prompt is there is none
  23. // fmt.Fprintln(os.Stderr, "Feed me words!")
  24. ste.WriteString("Feed me words!\n")
  25. ste.Flush()
  26. }
  27. for sti.Scan() {
  28. // Stop on an empty string
  29. if sti.Text() == "" {
  30. break
  31. }
  32. // Process the incoming strings
  33. titled := strings.Title(sti.Text())
  34. // fmt.Println(titled)
  35. sto.WriteString(titled + "\n")
  36. sto.Flush()
  37. }
  38. }