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:
// Golang program to show how // to take input from the user package main import "fmt" // main function func main() { // Println function is used to // display output in the next line fmt.Println("Enter Your First Name: ") // var then variable name then variable type var first string // Taking input from user fmt.Scanln(&first;) fmt.Println("Enter Second Last Name: ") var second string fmt.Scanln(&second;) // Print function is used to // display output in the same line fmt.Print("Your Full Name is: ") // Addition of two string fmt.Print(first + " " + second) } |
chevron_right
filter_none
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.
Recommended Posts:
- How to validate and sanitize user input with PHP?
- How to take user input for two dimensional (2D) array in PHP ?
- Taking Input from User in R Programming
- Take Matrix input from user in Python
- Convert user input string into regular expression using JavaScript
- CSS | User Interface
- How to Create a New User in Windows 10?
- How to get user location in Android
- How to hide a div when the user clicks outside of it using jQuery?
- Add a User in Linux using Python Script
- How to read a Matrix from user in Java?
- How to display logged in user information in PHP ?
- HTTP headers | User-Agent
- How to check the user is using Internet Explorer in JavaScript?
- How to create an unordered_map of user defined class in C++?
- How to create a user defined javap tool?
- Change Image Dynamically when User Scrolls Down | Set 2
- Deleting a User in Linux using Python Script
- How to display popup message when logged out user try to vote ?
- How to create an unordered_set of user defined class or struct in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

