Integers (int)
Part of the Fundamentals section of Coddy's GO journey — lesson 7 of 109.
Integers (int) are whole numbers without decimal points. Go uses int to store these values.
Let's create integer variable using the var keyword:
var age int = 25
fmt.Println("Age:", age)After executing this code, you will see Age: 25 displayed on the screen. We've created an integer variable named age with the value 25.
Now, let's create an integer using Go's short declaration syntax with :=:
score := 95
fmt.Println("Score:", score)After executing this code, you will see Score: 95 displayed on the screen. The := operator automatically determines that score should be an integer type based on the value 95.
Challenge
BeginnerIn this challenge, you'll practice working with integer variables in Go.
Integers (int) are whole numbers without decimal points. They can be positive, negative, or zero.
Your tasks:
- Create an integer variable named
agewith a value of 25 - Create another integer variable named
yearwith a value of 2023 - Create a third integer variable named
temperaturewith a value of -5 - Print all three variables on separate lines using
fmt.Println()
Cheat sheet
Integers (int) are whole numbers without decimal points. They can be positive, negative, or zero.
Create integer variables using the var keyword:
var age int = 25
fmt.Println("Age:", age)Create integers using Go's short declaration syntax with :=:
score := 95
fmt.Println("Score:", score)Try it yourself
package main
import "fmt"
func main() {
// Create your integer variables here
age := ?
year := ?
temperature := ?
// Print the variables on separate lines
fmt.Println(?)
fmt.Println(?)
fmt.Println(?)
}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