-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
58 lines (50 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Read evaluate print loop
Program keeps accepting the text input from STDIN,
process it (transform the text to upper case) and print
it on STDOUT
credit: opencensus.io
*/
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
)
func main() {
// 1. Read input
// 2. process input
br := bufio.NewReader(os.Stdin)
// repl is the read, evaluate, print, loop
for {
if err := readEvaluateProcess(br); err != nil {
if err == io.EOF {
return
}
log.Fatal(err)
}
}
}
// readEvaluateProcess reads a line from the input reader and
// then processes it. It returns an error if any was encountered.
func readEvaluateProcess(br *bufio.Reader) (terr error) {
fmt.Printf("> ")
line, _, err := br.ReadLine()
if err != nil {
return err
}
out, err := processLine(line)
if err != nil {
return err
}
fmt.Printf("< %s\n\n", out)
return nil
}
// processLine takes in a line of text and
// transforms it. Currently it just capitalizes it.
func processLine(in []byte) (out []byte, err error) {
return bytes.ToUpper(in), nil
}