part1.go 675 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "os"
  4. "log"
  5. "encoding/json"
  6. "bufio"
  7. )
  8. type Message struct {
  9. Body string
  10. }
  11. func main() {
  12. // TODO: Create a new bufio.Scanner reading from the standard input.
  13. s := bufio.NewScanner(os.Stdin)
  14. // TODO: Create a new json.Encoder writing into the standard output.
  15. e := json.NewEncoder(os.Stdout)
  16. /* TODO: Iterate over every line in the scanner */
  17. for s.Scan() {
  18. // TODO: Create a new message with the read text.
  19. // TODO: Encode the message, and check for errors!
  20. err := e.Encode(Message{Body: s.Text()})
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. }
  25. // TODO: Check for a scan error.
  26. if err := s.Err(); err != nil {
  27. log.Fatal(err)
  28. }
  29. }