go__whispering-gophers/part1/part1.go

35 lines
675 B
Go

package main
import (
"os"
"log"
"encoding/json"
"bufio"
)
type Message struct {
Body string
}
func main() {
// TODO: Create a new bufio.Scanner reading from the standard input.
s := bufio.NewScanner(os.Stdin)
// TODO: Create a new json.Encoder writing into the standard output.
e := json.NewEncoder(os.Stdout)
/* TODO: Iterate over every line in the scanner */
for s.Scan() {
// TODO: Create a new message with the read text.
// TODO: Encode the message, and check for errors!
err := e.Encode(Message{Body: s.Text()})
if err != nil {
log.Fatal(err)
}
}
// TODO: Check for a scan error.
if err := s.Err(); err != nil {
log.Fatal(err)
}
}