Menu
Coddy logo textTech

Getting Basic User Input

Part of the Fundamentals section of Coddy's GO journey — lesson 32 of 109.

Let's learn how to get basic user input in Go. We'll create a simple program that asks for a name and age, then displays a greeting.

First, let's set up variables to store the user's name and age:

var name string
var age int

These variables will hold the information that the user types in. The string type is for text, and int is for whole numbers.

Next, let's ask the user for their name:

fmt.Print("Enter your name: ")
fmt.Scan(&name)

After executing this code, the text "Enter your name:" will show on the screen. The program will then wait for the user to type something and press Enter.

Whatever they type will be stored in the name variable. The & symbol tells Go where to store the input.

Now, let's ask for the user's age:

fmt.Print("Enter your age: ")
fmt.Scan(&age)

Similar to the previous step, "Enter your age:" will show on the screen, and the program will wait for the user to type a number. This number will be stored in the age variable.

Finally, let's display a greeting using the information we collected:

fmt.Printf("Hello %s, you are %d years old!\n", name, age)

After executing this code, a personalized message will show on the screen.

For example, when you run this program and enter "Gopher" and "5", you'll see:

Enter your name: Gopher
Enter your age: 5
Hello Gopher, you are 5 years old!
challenge icon

Challenge

Beginner

In this challenge, you'll practice getting basic user input in Go. You'll use the fmt.Scanln() function to read input from the user and store it in a variable.

Your task is to complete the program that asks the user for their name and then greets them.

Note: This challenge uses predefined variables instead of user input.

Cheat sheet

To get user input in Go, declare variables and use fmt.Scan():

var name string
var age int

fmt.Print("Enter your name: ")
fmt.Scan(&name)

fmt.Print("Enter your age: ")
fmt.Scan(&age)

fmt.Printf("Hello %s, you are %d years old!\n", name, age)

The & symbol tells Go where to store the input. Use string type for text and int for whole numbers.

Try it yourself

package main

import "fmt"

func main() {
	// Declare a variable to store the user's name
	var name string
	
	// Display a prompt for the user
	fmt.Print("Enter your name: ")
	
	// TODO: Use fmt.Scanln() to read the user's input into the 'name' variable
	
	// Display a greeting using the name entered
	fmt.Printf("Hello, %s! Nice to meet you.", name)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals