How to get Input from the User in Golang?


In this tutorial, we will see how to get the input from the user in Golang. Golang has a library that consists of an input/output function which helps in printing and taking the output. The function to take the input is Scanln().

Algorithm

For Taking Integer Input with User

  • STEP 1 − We are importing the fmt package that has the input/output functions.

  • STEP 2 − We are declaring a variable after that we are printing the lines to ask the user to give the input

  • STEP 3 − Then using fmt.Scanln() we are taking the input and storing it into a variable

  • STEP 4 − We are checking the input from the user that its mode with 2 is zero or not using

Example 1

In this example, we will take an integer input from the user.

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the variable using the var keyword var numberFromUser int fmt.Println("Please enter the number you want to check that it is divisible by 2 or not:") // scanning the input by the user fmt.Scanln(&numberFromUser) // logic to check that number is divisible by 2 or not if numberFromUser%2 == 0 { fmt.Println(numberFromUser, "is divisible by 2") } else { fmt.Println(numberFromUser, "is not divisible by 2") } }

Output 1

Please enter the number you want to check that it is divisible by 2 or not:
33
33 is not divisible by 2

Output 2

Please enter the number you want to check that it is divisible by 2 or not:
28
28 is divisible by 2

Example 2

In this example, we will take a string input from the user.

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the float variables var number1, number2, answer float32 fmt.Println("Enter the numbers on which you want to perform arithmetic operators.") // scanning the input by the user fmt.Println("Enter the first number.") fmt.Scanln(&number1) fmt.Println("Enter the second number.") fmt.Scanln(&number2) // declaring the string variable using the var keyword var operator string fmt.Println("Enter the operator you want to perform on two numbers.") // scanning the input by the user fmt.Scanln(&operator) switch operator { case "+": answer = number1 + number2 fmt.Println("The addition of", number1, "and", number2, "is", answer) case "-": answer = number1 - number2 fmt.Println("The subtraction of", number1, "and", number2, "is", answer) case "*": answer = number1 * number2 fmt.Println("The multiplication of", number1, "and", number2, "is", answer) case "/": answer = number1 / number2 fmt.Println("The division of", number1, "and", number2, "is", answer) } }

In the above code, we are performing arithmetic operators where

  • first, we are declaring three float variables two of which store the input of numbers from the user, and the third one will store the answer after performing the arithmetic operation.

  • Now we are taking the arithmetic operator as input from the user.

  • In last we are using the switch case to compare the operator input and performing and printing the respective answer.

Output 1

Enter the numbers on which you want to perform arithmetic operators.
Enter the first number.
10
Enter the second number.
20
Enter the operator you want to perform on two numbers.
+
The addition of 10 and 20 is 30.

Output 2

Enter the numbers on which you want to perform arithmetic operators.
Enter the first number.
30
Enter the second number.
10
Enter the operator you want to perform on two numbers.
-
The subtraction of 30 and 10 is 20.

Output 3

Enter the numbers on which you want to perform arithmetic operators.
Enter the first number.
30
Enter the second number.
5
Enter the operator you want to perform on two numbers.
*
The multiplication of 30 and 5 is 150.

Output 4

Enter the numbers on which you want to perform arithmetic operators.
Enter the first number.
10
Enter the second number.
2
Enter the operator you want to perform on two numbers.
/
The division of 10 and 2 is 5.

Example 3

Take two string words as input.

package main import ( "bufio" "fmt" "os" "strings" ) func main() { // declaring the variable using the var keyword var capitalOfSouthAfrica string fmt.Println("What is the capital of South Africa") fmt.Println("Please enter your answer:") // scanning the input by the user inputReader := bufio.NewReader(os.Stdin) capitalOfSouthAfrica, _ = inputReader.ReadString('\n') // remove the delimiter from the string capitalOfSouthAfrica = strings.TrimSuffix(capitalOfSouthAfrica, "\n") if capitalOfSouthAfrica == "Capetown" { fmt.Println("You are right!") } else { fmt.Println("Sorry,the wrong answer, it is not", capitalOfSouthAfrica, ", it's Capetown.") } }

In the above example first, we are importing the bufio package that has the input/output functions. Then inside the main function, we are declaring a string variable after that we are printing the lines to ask the user to give the input. Then using bufio we are creating an input reader object which is in the next line reading the string input and storing it into a variable. In last we are comparing the input from the user to Capetown using the if condition and print accordingly if the user has given the right input or not.

Output 1

What is the capital of South Africa
Please enter your answer:
Capetown
You are right!

Output 2

What is the capital of South Africa
Please enter your answer:
New York
Sorry, the wrong answer, it is not New York, it's Capetown.

This is all about the libraries and the function used to take the input from the user. To learn more about Golang you can explore this tutorials.

Updated on: 26-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements