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 intThese 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
BeginnerIn 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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions