Slicing Existing Slices/Arrays
Part of the Fundamentals section of Coddy's GO journey — lesson 80 of 109.
You can create a new slice from an existing slice or array by specifying a range with the [start:end] syntax.
Create a slice of numbers:
numbers := []int{10, 20, 30, 40, 50}
fmt.Println("Original:", numbers)Extract elements from index 1 to 3 (excluding 3):
slice1 := numbers[1:3]
fmt.Println("Slice [1:3]:", slice1)Result:
Original: [10 20 30 40 50]
Slice [1:3]: [20 30]You can also omit start or end index:
slice2 := numbers[:2] // From start to index 2 (excluding 2)
slice3 := numbers[3:] // From index 3 to end
fmt.Println("Slice [:2]:", slice2)
fmt.Println("Slice [3:]:", slice3)Result:
Slice [:2]: [10 20]
Slice [3:]: [40 50]Challenge
BeginnerIn this challenge, you'll practice slicing an existing slice to create new sub-slices. You have a slice of fruits, and you need to create two new slices from it:
- A slice containing only the first three fruits
- A slice containing only the last two fruits
Then print both new slices to verify your work.
Cheat sheet
Create a new slice from an existing slice or array using [start:end] syntax:
numbers := []int{10, 20, 30, 40, 50}
slice1 := numbers[1:3] // Elements from index 1 to 3 (excluding 3)
fmt.Println(slice1) // [20 30]You can omit start or end index:
slice2 := numbers[:2] // From start to index 2 (excluding 2)
slice3 := numbers[3:] // From index 3 to end
fmt.Println(slice2) // [10 20]
fmt.Println(slice3) // [40 50]Try it yourself
package main
import "fmt"
func main() {
// Here's our original slice of fruits
fruits := []string{"apple", "banana", "orange", "grape", "kiwi"}
// TODO: Create a new slice called 'firstThree' containing only the first three fruits
// TODO: Create a new slice called 'lastTwo' containing only the last two fruits
// Print the results
fmt.Println("First three fruits:", firstThree)
fmt.Println("Last two fruits:", lastTwo)
}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 Code11Composite Types: Slices
Introduction to SlicesDeclaring Slice LiteralsCreating Slices with `make`Slice Length vs CapacityAccessing Slice ElementsAppending ElementsSlicing Existing Slices/ArraysIterating Over SlicesCopying SlicesRecap - Dynamic Lists3Basic 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