Division Operator
Part of the Fundamentals section of Coddy's GO journey — lesson 16 of 109.
The division operator (/) divides the left operand by the right operand.
Create two variables and perform division:
a := 10
b := 3
result := a / b
fmt.Println(result)Output:
3Note that when dividing integers, Go truncates the result (removes decimal part).
For decimal division, use floating-point numbers:
x := 10.0
y := 3.0
decimalResult := x / y
fmt.Println(decimalResult)Output:
3.3333333333333335Challenge
BeginnerComplete the code to perform the division and print the result.
Cheat sheet
The division operator (/) divides the left operand by the right operand:
a := 10
b := 3
result := a / b
fmt.Println(result) // Output: 3When dividing integers, Go truncates the result (removes decimal part).
For decimal division, use floating-point numbers:
x := 10.0
y := 3.0
decimalResult := x / y
fmt.Println(decimalResult) // Output: 3.3333333333333335Try it yourself
package main
import "fmt"
func main() {
// These numbers are already defined for you
number1 := 20.0
number2 := 4.0
// TODO: Divide number1 by number2 and store the result in the 'result' variable
var result float64
// This will print the result
fmt.Println("The result of", number1, "divided by", number2, "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