I want to display a "> ", and then have the program wait for the user to type something into the command line and hit enter (and ultimately store that as a byte slice). In Java I would have done something like:
Scanner scanner = new Scanner(System.in);
System.out.print("> ");
String sentence = scanner.nextLine();
I've tried some things involving os.Args, console.ReadBytes('\n')
, and bufio.NewReader(os.Stdin)
, but I still haven't figured it out. Any suggestions would be greatly appreciated. Thanks.
Some Go code would have probably been helpful so I could tell you what you did wrong. But here is how you would do this in Go:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
buf := bufio.NewReader(os.Stdin)
fmt.Print("> ")
sentence, err := buf.ReadBytes('\n')
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(sentence))
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With