Golang: Input
Introduction
In this fourth section of Golang, we will be understanding the basics of user input. In golang, we can get user input with several functions most of which are similar to the C
programming language like scanf
. This type of input is quite powerful and gives more control on the input to be received.
Scan Function
The Scan function helps in getting a value with space as delimiter i.e. The input is stored before a space is encountered. This means the input is only limited to adding a space or a new line. We can use the function by passing the reference to the variable we are going to store the input value. So, we can have a basic input in Golang as follows:
package main
import "fmt"
func main() {
var pname string
fmt.Println("Enter your favourite programming language: ")
fmt.Scan(&pname)
fmt.Println("So, your favourite programming language is",pname)
}
$ go run scan.go
Enter your favorite programming language:
python
So, your favorite programming language is python
We need to declare the variable to take input as we need a reference of that variable to store the input. We will be talking about &
and pointers in a separate article. We use the Scan
function by passing the reference to the variable pname
like &pname
which means, fetch the memory address of the variable name
, we just pass the address as int
to the Scan function and it does the rest to store the input value in it. We then as usual access the variable and operations on it.
Here, if you add a space in the input, the value after the space won’t be picked by the Scan function. It strictly stops accepting values input after it sees space. We can use this to input multiple variables at once. We know scan gets input before encountering space, so we can pass multiple variable references and add them as input.
var (
name string
age int
gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scan(&name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
$ go run scan.go
Enter your name age and gender:
Meet 19 77
Hello Meet, you are a M and 19 years old
Here, we are declaring multiple variables like name
, age
, and gender
as string
, int
, and rune
respectively. Then, we can input all of these in a single scan statement by comma-separated variables. Here, we need to input the rune
as an int value because under the hood it is an integer alias. So, we inputted 77
which is equivalent to M
in ASCII characters and even Unicode character sets. Thus, we were able to input multiple variables with the Scan function.
Scanf functions
The Scanf function is quite similar to the scanf
in C programming language as it allows to specify the type of the incoming input. This will solve the problem of us inputting 77
instead of M
in the gender variable in the previous example. The Scanf function allows us to take input by specifying the placeholder types and the delimiters as well. The delimiter is basically the separator between two or more entities. We can either use space separation or \n
as an input delimiter i.e. the way we want to separate inputs from each other while taking input.
var (
name string
age int
gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scanf("%s %d %c", &name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
$ go run scanf.go
Enter your name age and gender:
Meet 12 M
Hello Meet, you are a M and 12 years old
How cool is that? It definitely gives much more control on what and how to take input. We are taking input as only space-separated values. Let’s now try to get more control over how the input will be taken and stored.
var (
name string
age int
gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scanf("%s \n %d %c", &name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
$ go run scanf.go
Enter your name age and gender:
Meet
12 M
Hello Meet, you are a M and 12 years old
By adding \n
between the %s
(name) and %d
(age), we want the user to type the name on one line and age with gender on a different line. The age and gender as before separated by space.
Scanln function
The Scanln function is a modification of the Scan function as it only stops the input after a newline/enter is pressed. So, using this we can input multiple variables which are space-separated in a single line.
var s string
fmt.Println("Enter a string: ")
fmt.Scanln(&s)
fmt.Println(s)
$ go run scanln.go
Enter a string:
$ go run scanln.go
Enter a string:
Can't type
Can't
$ ype
-bash: ype: command not found
The Scanln function even accepts an empty string as input. It just needs to get the new line character and it will exit, it also only accepts space-separated values. The rest of the input after space is thrown away and is basically exited from the program stream. More specifically, the input Can't Type
was treated only as Can't
anything after the space is not considered in the input value.
The key difference between Scan and Scanln is that Scanln will not accept input that is space-separated, Scan function considers the newline/enter as a space if there are multiple inputs. The below example will make things absolutely clear.
// scan.go
package main
import "fmt"
func main() {
var (
name string
age int
gender rune
)
fmt.Println("Enter your name age and gender: ")
fmt.Scan(&name, &age, &gender)
fmt.Printf("Hello %s, you are a %c and %d years old", name, gender, age)
}
//scanln.go
package main
import "fmt"
func main() {
var s string
fmt.Println("Enter a string: ")
fmt.Scanln(&s)
fmt.Println("Inputted string : ", s)
}
$ go run scan.go
Enter your name age and gender:
Meet
14
77
Hello Meet, you are a M and 14 years old
$ go run scanln.go
Enter a string:
Inputted string :
We can see that, The Scan function won’t exit until it has inputted all its input values even with newline and spaces. Whereas the Scanln function just waits for the newline character (Enter Key) to be pressed and it exits, thereby even allowing an empty string as input.
That’s it from this part. Reference for all the code examples and commands can be found in the 100 days of Golang GitHub repository.
Conclusion
So, these are the basic input techniques in Golang. We saw functions in the fmt
package like Scan
, Scanf
, and Scanln
which allow us to get input in a specific pattern. Hopefully, from this article part, we can build a firm base for further exploration like Strings, Arrays, and the ways to input them. Thank you for reading. If you have any questions or feedback, please let me know in the comments or on social handles. Happy Coding :)