How to Take Input from the User in Golang?
Scanln function can be used to take the input from the user in the Golang. Below is the example of taking input from the user:
Now save this file and execute as shown in the below screenshot:
Description About the Program:
- main Package: When we build reusable pieces of code, we will develop a package as a shared library. But when we develop an executable program, we will use the package “main” for making the package as an executable program. The package “main” tells the Golang compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program. Remember when we build shared libraries, we will not have any main package and main function in the package.
- fmt.Println is the print function which is used to print the output in the next line. While fmt.Print is used to display output in the same line. Whatever needs to be printed has to be written in inverted commas ” “.
- var first string is the declaration of the variable first which is of string type. To declare variables following syntax needs to be followed:
var var_name data_type
- fmt.Scanln(&first) is used to take input from user in the next line. While fmt.Scan is used to take input from user in the same line. Ampersand is necessary to give the reference as to in which variable we have to store the variable.
- The Last line will simply add the two string of First Name and Last Name and will output the Full Name.
My Personal Notes arrow_drop_up
Please Login to comment...