40 lines
675 B
Go
40 lines
675 B
Go
// Solution to part 1 of the Whispering Gophers code lab.
|
|
// This program reads from standard input and writes JSON-encoded messages to
|
|
// standard output. For example, this input line:
|
|
// Hello!
|
|
// Produces this output:
|
|
// {"Body":"Hello!"}
|
|
//
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Message struct {
|
|
Body string
|
|
}
|
|
|
|
func main() {
|
|
// Defaults to ScanLines
|
|
in := bufio.NewScanner(os.Stdin)
|
|
// in.Split(bufio.ScanLines)
|
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
|
|
for in.Scan() {
|
|
m := Message{
|
|
Body:in.Text(),
|
|
}
|
|
err := encoder.Encode(m)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
if err := in.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|