...

/

Important Characteristics of Go: Getting User Input

Important Characteristics of Go: Getting User Input

Let’s learn how to get user input and how to read from standard input in Go.

Getting user input is an important part of every program. This lesson presents two ways of getting user input, which are:

  • Reading from standard input.

  • Using the command-line arguments of the program.

Reading from standard input

The fmt.Scanln() function can help us read user input while the program is already running and store it to a string variable, which is passed as a pointer to fmt.Scanln(). The fmt package contains additional functions for reading user input from the console (os.Stdin), from files, or from argument lists.

Let's try our code

The following program input.go illustrates reading from standard input:

package main

import (
	"fmt"
)

func main() {
	// Get User Input
	fmt.Printf("Please give me your name: ")
	var name string
	fmt.Scanln(&name)
	fmt.Println("Your name is", name)
}
input.go

While waiting for user input, it is good to let the user know what kind of information they have to give, which is the purpose of ...