Arithmetic Operators
Part of the Fundamentals section of Coddy's GO journey — lesson 15 of 109.
Go provides standard arithmetic operators for mathematical operations.
Create variables and perform addition with +:
a := 5
b := 3
sum := a + b
fmt.Println(sum)Output:
8If the variable is already declared, use = instead of := to assign a new value:
a := 5
b := 3
var sum int
sum = a + b
fmt.Println(sum)Output:
8Perform subtraction with -:
difference := a - b
fmt.Println(difference)Output:
2If the variable is already declared, use = to reassign:
var difference int
difference = a - b
fmt.Println(difference)Output:
2Perform multiplication with *:
product := a * b
fmt.Println(product)Output:
15If the variable is already declared, use = to reassign:
var product int
product = a * b
fmt.Println(product)Output:
15Challenge
BeginnerIn this challenge, you'll practice using arithmetic operators in Go. We have three predefined variables: num1, num2, and result.
Your task is to calculate the sum of num1 and num2, then multiply that result by 2, and store the final answer in the result variable.
Cheat sheet
Go provides standard arithmetic operators for mathematical operations:
Addition (+):
a := 5
b := 3
sum := a + b
fmt.Println(sum) // Output: 8Subtraction (-):
difference := a - b
fmt.Println(difference) // Output: 2Multiplication (*):
product := a * b
fmt.Println(product) // Output: 15Declaration vs. Assignment:
Use := to declare a new variable and assign a value at the same time.
Use = to assign a new value to an already-declared variable:
sum := 0 // declares a new variable
sum = a + b // reassigns an existing variable
fmt.Println(sum) // Output: 8Try it yourself
package main
import "fmt"
func main() {
// These variables are already defined for you
num1 := 5
num2 := 7
result := 0
// TODO: Calculate (num1 + num2) * 2 and store it in the result variable
// This will print the result
fmt.Println("The result is:", result)
}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