Go User Input
Go programming provides three different scan methods that can be used to read/take user inputs:
fmt.Scan()
fmt.Scanln()
fmt.Scanf()
Note: All the three functions are from the fmt package, you need to import the fmt
package before using it in your program.
The Scan
function
As promised, we will now see how to ask the user for input.
In Go, this is done using the Scan
function from the fmt
package.
A simple example is the following:
package main
import "fmt"
func main() {
var name string
fmt.Print("What is your name? ")
fmt.Scan(&name)
fmt.Printf("Hello, %s!\n", name)
}
Output
What is your name? Chandler Bing
Hello, Chandler!
- You declare a variable to store the input.
- You print a message to ask the user for input.
- You call the
Scan
function with the address of the variable as an argument.
An address is a value that uniquely identifies a location in memory. You can have two variables that share the same address if they are stored in the same location in memory.
Notice - We have provided the input as Chandler Bing to the Scan()
function in our program. However we are only getting Chandler
as the output. This is beacuse the Scan()
method takes input until it finds the space in input string. So in our example after Chandler there is a space and hence the last name was not taken by the Scan()
method
How to Read Multiple Inputs Using Scan() Method
The Scan()
function can take multiple inputs from the user, let us take an example to demonstrate how this works.
package main
import "fmt"
func main() {
var fname string
var lname string
fmt.Println("Please enter your First Name and Last Name: ")
fmt.Scan(&fname, &lname)
fmt.Printf("Hello, %s %s!\n", fname, lname)
}
Output
Please enter your First Name and Last Name:
Chandler
Bing
Hello, Chandler Bing!
Notice -
- We are using a single
Scan()
function to read bothfname
andlname
- We are providing two inputs values first name and last name to the
Scan()
- We have provided each input values in two different lines/newlines.
- The
Scan()
function reads the input from two different lines and assigns to respective variables
The another way to pass values to the Scan()
method is by provding both the values in a single line with space as a separator.
Please enter your First Name and Last Name:
Chandler Bing
Hello, Chandler Bing!
Here the first value before space is assigned to fname
variable and the value after space is assigned to lname
variable.
Rewriting the conversion program
Let us rewrite the conversion program from the previous chapter to use the Scan
function instead of hard-coding the temperature:
package main
import "fmt"
func main() {
var celcius float64
fmt.Print("Enter a temperature in Celsius: ")
fmt.Scan(&celcius)
fahrenheit := celcius*9/5 + 32
fmt.Printf("%.2f°C = %.2f°F\n", celcius, fahrenheit)
}
Output
Enter a temperature in Celsius: 37
37.00°C = 98.60°F
Note: fahrenheit
is no longer a constant because its value is not known at compile time, it depends on the user input.
The Scanln
function
The Scan
function only reads a single word, delimited by a space or a newline.
If you want to read an entire line, you can use Scanln
instead (from the same package):
package main
import "fmt"
func main() {
var fname string
var lname string
fmt.Println("Please enter your First Name and Last Name: ")
fmt.Scanln(&fname, &lname)
fmt.Printf("Hello, %s %s!\n", fname, lname)
}
Output
Please enter your First Name and Last Name:
Chandler Bing
Hello, Chandler Bing!
Please enter your First Name and Last Name:
Chandler
Hello, Chandler !
Note- Unlike Scan()
function the Scanln()
method cannot take multiple inputs in new lines. If you want to read multiple inputs in Scanln()
method we need to seperate it by space. We cannot pass the inputs in two different lines, the moment Scanln()
encouters enter it stops taking further input and the statement gets terminated.
The Scanf
function
Finally, you can use the Scanf()
function to ask for input in a specific format. Below are the format specifiers for various data types.
%d
for integers,%f
for floating-point numbers (you can specify the number of digits after the decimal point, e.g.%.2f
for two digits),%s
for strings,%t
for booleans.
For example, if you want to read a number followed by a unit, you can use the following code:
package main
import "fmt"
func main() {
var value float64
var unit string
fmt.Print("Enter a value and a unit (e.g. 12.3kg): ")
fmt.Scanf("%f%s", &value, &unit)
fmt.Printf("You entered: %.2f%s\n", value, unit)
}
Output
Enter a value and a unit (e.g. 12.3kg): 76.4 pounds
You entered: 76.40pounds
It is less commonly used than the other two functions, but it still has its uses.
Note- Unlike Scan()
function the Scanf()
method cannot take multiple inputs in new lines. If you want to read multiple inputs in Scanf()
method we need to seperate it by space. We cannot pass the inputs in two different lines, the moment Scanf()
encouters enter it stops taking further input and the statement gets terminated.