Declaring Arrays
Part of the Fundamentals section of Coddy's GO journey — lesson 68 of 109.
In Go, you can declare arrays in several ways. An array's size is part of its type and must be specified when declared.
Declare an array using var with a specific size:
var scores [4]int
fmt.Println(scores)This creates an array of 4 integers, all initialized to zero:
[0 0 0 0]Declare and initialize an array in one line using shorthand notation:
names := [3]string{"Alice", "Bob", "Charlie"}
fmt.Println(names)This displays:
[Alice Bob Charlie]Challenge
BeginnerCheat sheet
Arrays in Go have a fixed size that must be specified when declared. The size is part of the array's type.
Declare an array with var:
var scores [4]intDeclare and initialize an array using shorthand notation:
names := [3]string{"Alice", "Bob", "Charlie"}Arrays are initialized to zero values if not explicitly set:
[0 0 0 0]Try it yourself
package main
import "fmt"
func main() {
// TODO: Declare an array named 'numbers' that contains 5 integers: 10, 20, 30, 40, and 50
// This will print your array
fmt.Println("My array of numbers:", numbers)
}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