From the course: Learning Go
Get input from the console
- [Instructor] I've previously shown how to use functions from the "fmt" package to output strings to the console. Next, I'll show you how to get user input, allowing user to type values on the command line. I'm working in the main.go file in the practice directory. And in this branch, I've reduced the amount of code that we're starting with. To get input, you need a package called "bufio". I'll add that to my imports. And I also need one called "os". Now, I'll go to my main function and I'll delete the existing code. And I'll create a new variable that I'll name reader. I'll initialize that variable with bufio.NewReader. This function requires a parameter indicating where the data's going to come from. And I'll use an expression of os.Stdin. That stands for standard input, what the user types on the command line. Now, I want to display a prompt to the user. I'll use fmt.Print. Notice I'm not using print line because I don't want a line feed at the end of the string. And I'll pass in a prompt of "Enter text:" being sure to include a space at the end. Now I want the application to pause and let the user type something in. I'm going to be creating two variables. The first will be a string, whatever the user types, and I'll name that input. The second variable which will also be returned at the same time, is potentially an error object. I'll talk about error handling later on. But for now, what we need to know is that if you want to ignore a variable, name it with the underscore character. I'll initialize those variables with reader.ReadString. This function requires a single character. A single byte. This is the delimiter and it tells the ReadString function when to stop accepting input. I'll pass in a single character of '\n' align feed. I'm wrapping that in single quotes because in go, just like in Java, characters are wrapped in single quotes, while full strings are wrapped in double quotes. Now I'll print out what was entered. I'll start with a string of "You entered:" And then after a comma, I'll pass in the input value. Now, if I try to run this application from within visual studio code, it isn't going to work out too well. Notice I'm seeing my prompt here. But if I try to click into the debug console, I won't be able to. So I'm going to go up here where the application has been suspended, and therefore I'm getting this tool bar, and I'll click this square box, meaning I want to stop the application. Then, I'll right click on the file in the explore window, and choose open an integrated terminal. I'll list the contents of the directory to make sure I'm in the right place. Then I'll run the application with go run . I see the prompt to enter some text. I'll type here's some text. And I get the feedback you entered here some text. So that's how you can do basic input from a command line application in go. You're using the new reader function in the "bufio" package to get a reader object and then Stdin from the "OS" package to indicate that you're getting the data from what the user types in.