Initializing Arrays
Part of the Fundamentals section of Coddy's GO journey — lesson 69 of 109.
Initializing Arrays means setting initial values when creating an array.
Initialize an array with specific values using curly braces:
scores := [4]int{95, 87, 78, 92}
fmt.Println(scores)This displays:
[95 87 78 92]Use the ellipsis (...) to let Go count the elements for you:
colors := [...]string{"Red", "Green", "Blue"}
fmt.Println(colors)
fmt.Println("Array length:", len(colors))This outputs:
[Red Green Blue]
Array length: 3Challenge
BeginnerIn this challenge, you'll practice initializing arrays in Go. Your task is to create an array of 5 integers with the specific values provided, then print the array.
The code already has the necessary imports and a main function. You just need to initialize the array with the given numbers.
Cheat sheet
Initialize an array with specific values using curly braces:
scores := [4]int{95, 87, 78, 92}Use the ellipsis (...) to let Go count the elements automatically:
colors := [...]string{"Red", "Green", "Blue"}Get array length with len():
fmt.Println("Array length:", len(colors))Try it yourself
package main
import "fmt"
func main() {
// TODO: Initialize an array called 'favoriteNumbers' with these 5 integers: 7, 42, 8, 13, 99
// You can use either syntax:
// favoriteNumbers := [5]int{7, 42, 8, 13, 99}
// OR
// var favoriteNumbers [5]int = [5]int{7, 42, 8, 13, 99}
// This will print your array
fmt.Printf("My favorite numbers are: %v\n", favoriteNumbers)
}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 Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays2Variables 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