Accessing Array Elements
Part of the Fundamentals section of Coddy's GO journey — lesson 70 of 109.
To access array elements, use square brackets with an index number. Array indices start at 0.
Create an array of names:
names := [3]string{"Alex", "Beth", "Carlos"}Access individual elements:
firstPerson := names[0]
secondPerson := names[1]
fmt.Println(firstPerson)
fmt.Println(secondPerson)This displays:
Alex
BethYou can also modify elements:
names[2] = "Charlie"
fmt.Println(names[2])This outputs:
CharlieChallenge
BeginnerComplete the code to:
1. Access the first fruit in the array
2. Access the third fruit in the array
3. Print both fruits using fmt.Println
Cheat sheet
To access array elements, use square brackets with an index number. Array indices start at 0.
Create an array:
names := [3]string{"Alex", "Beth", "Carlos"}Access individual elements:
firstPerson := names[0]
secondPerson := names[1]Modify elements:
names[2] = "Charlie"Try it yourself
package main
import "fmt"
func main() {
// This is our array of fruits
fruits := [5]string{"apple", "banana", "cherry", "date", "elderberry"}
// TODO: Create a variable called firstFruit and assign it the first element of the fruits array
// TODO: Create a variable called thirdFruit and assign it the third element of the fruits array
// Print the fruits
fmt.Println("The first fruit is:", firstFruit)
fmt.Println("The third fruit is:", thirdFruit)
}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