main.go 675 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Solution to part 1 of the Whispering Gophers code lab.
  2. // This program reads from standard input and writes JSON-encoded messages to
  3. // standard output. For example, this input line:
  4. // Hello!
  5. // Produces this output:
  6. // {"Body":"Hello!"}
  7. //
  8. package main
  9. import (
  10. "bufio"
  11. "encoding/json"
  12. "log"
  13. "os"
  14. )
  15. type Message struct {
  16. Body string
  17. }
  18. func main() {
  19. // Defaults to ScanLines
  20. in := bufio.NewScanner(os.Stdin)
  21. // in.Split(bufio.ScanLines)
  22. encoder := json.NewEncoder(os.Stdout)
  23. for in.Scan() {
  24. m := Message{
  25. Body:in.Text(),
  26. }
  27. err := encoder.Encode(m)
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. }
  32. if err := in.Err(); err != nil {
  33. log.Fatal(err)
  34. }
  35. }