How do i take input from User in Go Lang

Last updated Oct 22, 2021

In this Go example tutorial we will cover take user input in Go Lang. The user input can be added by using bufio package and fmt package.

 

Create an Input with bufio Package

This is the basic syntax to create an input in Go with bufio package.

// create an input reader
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

// get the input
var_name, err := reader.ReadString(delimiter)

 

In this example, the input is used in this program.

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    // create an input reader
    var reader *bufio.Reader = bufio.NewReader(os.Stdin)

    fmt.Println("Enter your name: ")
    // receive an input
    // then save it to the name variable
    name, _ := reader.ReadString('\n')

    fmt.Println("Enter you age: ")
    // receive an input
    // then save it to the age variable
    age, _ := reader.ReadString('\n')

    fmt.Println("Hello, ", name)
    fmt.Println("Your age is: ", age)
}

Output

Enter your name:
aiden jackson
Enter you age:
25
Hello,  aiden jackson

Your age is:  25

 

Based on the code above, the reader is created then the input is received using ReadString('\n') method. The inputs are saved in name and age variable then the value from these variables is printed out.

 

Create an Input with fmt Package

This is the basic syntax to create an input with fmt package.

fmt.Scanln(&var_name)

 

 

How do we calculate sum of two number from user input using Go Lang?

package main

import (
    "fmt"
)

func main() {
    // create some variables to store the inputs
    var firstNum int
    var secondNum int

    // receive some inputs
    fmt.Println("Enter first number: ")
    fmt.Scanln(&firstNum)

    fmt.Println("Enter second number: ")
    fmt.Scanln(&secondNum)

    // calculate the sum of two numbers
    var result int = firstNum + secondNum

    // print out the result
    fmt.Println("The result: ", result)
}

Output

Enter first number:
5
Enter second number:
7
The result:  12

 

Based on the code above, the input is received with Scanln() method with variable name as the parameter.

 

How do we calculate average of numbers from user input using Go Lang?

 

package main

import (
    "fmt"
)

func main() {
    // create some variables
    var n int
    var data int

    fmt.Println("Average Calculator")
    fmt.Println("Enter the amount of data: ")
    // receive the amount of data
    fmt.Scanln(&n)

    var nums []int = []int{}

    for i := 1; i <= n; i++ {
        fmt.Println("Enter the number: ")
        // receive the numbers
        fmt.Scanln(&data)
        nums = append(nums, data)
    }

    // calculate the average then print out the result
    var averageResult int = average(nums)
    fmt.Println("Average result: ", averageResult)
}

// create a function to calculate the average
func average(nums []int) int {
    var sum int = 0
    for _, num := range nums {
        sum += num
    }

    var result int = sum / len(nums)
    return result
}

Output

Average Calculator
Enter the amount of data:
5
Enter the number:
1
Enter the number:
2
Enter the number:
3
Enter the number:
4
Enter the number:
5
Average result:  3

Based on the code above, the amount of data and data that will be calculated is received from the user input. Then the average calculation is performed.

 

I hope this article is helpful to learn how to create an user input in Go programming language.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1087 Views