From the course: Learning Go
Unlock this course with a free trial
Join today to access over 24,200 courses taught by industry experts.
Get input from the console - Go Tutorial
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…
Contents
-
-
-
-
Explore Go's variable types3m 16s
-
(Locked)
Declare and initialize variables5m 55s
-
(Locked)
Get input from the console3m 22s
-
(Locked)
Convert string inputs to other types4m 10s
-
(Locked)
Use math operators2m 3s
-
(Locked)
Use the math package5m 20s
-
(Locked)
Work with dates and times3m 50s
-
(Locked)
Solution: Create a simple calculator app2m 26s
-
-
-
-
-
-