-
I wrote a script mimicking the behavior of interactive programs by reading from stdin. The script: for {
printf("> ")
line := string(os.stdin.read())
switch line {
case "generate":
print("generate")
case "exit":
os.exit(0)
default:
printf("unknown command: %q\n", line)
}
} This has a major drawback of not recognizing the newline as the "stop" character. I wonder whether I am missing some information that I glazed over reading the documentation. In Go this would be implemented as such (taken from SO): reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println(text) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It seems that
|
Beta Was this translation helpful? Give feedback.
It seems that
os.stdin.read()
without argument will read until the EOF, so it stops only after ctrl-D. This program seems to do what you want, passing a byte slice toread
so that it fills it until a newline is found: