What is a variable
Part of the Fundamentals section of Coddy's GO journey — lesson 5 of 109.
A variable in Go is a named storage location that holds a value. Think of it like a labeled box where you can store information that your program needs to remember.
To create variables, use the var keyword with explicit types:
var name string = "Gopher" // Creates a string variable
var age int = 5 // Creates an integer variable
var price float64 = 19.99 // Creates a decimal number variableAfter executing this code, we have three variables: a string called name, an integer called age, and a float (decimal number) called price.
To create variables using var and to let Go figure out the type for us we can write without the types:
var name = "Gopher" // Go knows this is a string
var age = 5 // Go knows this is an integer
var price = 19.99 // Go knows this is a float64After executing this code, we get the same result as before, but Go automatically determined the types based on the values.
Challenge
Beginnervar keyword in Go. Your task is to declare two variables: name as a string with the value "Gopher" and age as an integer with the value 5. Then, the program will print these values.Cheat sheet
A variable in Go is a named storage location that holds a value. Use the var keyword to create variables.
With explicit types:
var name string = "Gopher" // Creates a string variable
var age int = 5 // Creates an integer variable
var price float64 = 19.99 // Creates a decimal number variableWithout explicit types (Go infers the type):
var name = "Gopher" // Go knows this is a string
var age = 5 // Go knows this is an integer
var price = 19.99 // Go knows this is a float64Try it yourself
package main
import "fmt"
func main() {
// TODO: Declare a variable named 'name' as a string with value "Gopher" using var
var name string = ?
// TODO: Declare a variable named 'age' as an int with value 5 using var
var age int = ?
// Don't modify the code below
fmt.Println("My name is", name, "and I am", age, "years old.")
}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