-
-
Save akvanhar/423e376aa028e630e4721f5551bd15b8 to your computer and use it in GitHub Desktop.
REPL example app with print statement logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"time" | |
) | |
func main() { | |
log.Printf("Started the program") | |
br := bufio.NewReader(os.Stdin) | |
count := 1 | |
for { | |
log.Printf("Starting entry %d", count) | |
count++ | |
if err := readEvaluateProcess(br); err != nil { | |
if err == io.EOF { | |
return | |
} | |
log.Printf("%s: A fatal error occured.", time.Now()) | |
log.Printf("%s", err) | |
} | |
} | |
} | |
func readEvaluateProcess(br *bufio.Reader) error { | |
t1 := time.Now() | |
fmt.Printf("> ") | |
line, err := readLine(br) | |
if err != nil { | |
log.Printf("%s: The following error occured: %s", time.Now(), err) | |
return err | |
} | |
out, err := processLine(line) | |
if err != nil { | |
log.Printf("%s: The following error occured: %s", time.Now(), err) | |
return err | |
} | |
fmt.Printf("< %s\n\n", out) | |
t2 := time.Now() | |
duration := int64(t2.Sub(t1) / time.Millisecond) | |
log.Printf("%s: duration in milliseconds: %d", time.Now(), duration) | |
return nil | |
} | |
func readLine(br *bufio.Reader) ([]byte, error) { | |
line, _, err := br.ReadLine() | |
log.Printf("%s: Read line. Text entered: %s", time.Now(), line) | |
if err != nil { | |
log.Printf("%s: The following error occured in readLine: %s", time.Now(), line) | |
return nil, err | |
} | |
return line, nil | |
} | |
func processLine(in []byte) (out []byte, err error) { | |
output := bytes.ToUpper(in) | |
log.Printf("%s: Process line. Text output: %s", time.Now(), output) | |
return output, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment