Assignment Operator
Part of the Fundamentals section of Coddy's GO journey — lesson 18 of 109.
The assignment operator (=) assigns a value to a variable. It's how we store new values in existing variables.
Create a variable and assign a value:
var score int
score = 100
fmt.Println(score)Output:
100You can also reassign values to change a variable:
score = 150
fmt.Println("Updated score:", score)Output:
Updated score: 150Challenge
BeginnerIn this challenge, you'll practice using the assignment operator (=) in Go.
We've created some variables for you. Your task is to use the assignment operator to change the value of the favoriteColor variable to "blue" and then print it out.
Cheat sheet
The assignment operator (=) assigns a value to a variable:
var score int
score = 100
fmt.Println(score) // Output: 100You can reassign values to change a variable:
score = 150
fmt.Println("Updated score:", score) // Output: Updated score: 150Try it yourself
package main
import "fmt"
func main() {
// This variable is already declared
favoriteColor := "red"
// TODO: Use the assignment operator (=) to change favoriteColor to "blue"
// This will print the value of favoriteColor
fmt.Println("My favorite color is:", favoriteColor)
}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