Copying Slices
Part of the Fundamentals section of Coddy's GO journey — lesson 82 of 109.
The copy function in Go lets you copy elements from one slice to another.
Create a source slice and a destination slice:
source := []int{1, 2, 3}
dest := make([]int, 2) // Destination with length 2
// Copy from source to destination
copied := copy(dest, source)
fmt.Println("Source:", source)
fmt.Println("Destination:", dest)
fmt.Println("Number of elements copied:", copied)Result:
Source: [1 2 3]
Destination: [1 2]
Number of elements copied: 2The copy function copies only as many elements as will fit in the destination slice.
Challenge
BeginnerIn this challenge, you'll practice using the copy function to copy elements from one slice to another.
You have two slices: source containing fruit names and destination which is an empty slice with capacity for 3 elements. Your task is to copy the elements from source to destination using the copy function.
After copying, print the contents of the destination slice.
Cheat sheet
The copy function copies elements from one slice to another:
source := []int{1, 2, 3}
dest := make([]int, 2) // Destination with length 2
// Copy from source to destination
copied := copy(dest, source)
fmt.Println("Source:", source)
fmt.Println("Destination:", dest)
fmt.Println("Number of elements copied:", copied)The copy function copies only as many elements as will fit in the destination slice and returns the number of elements copied.
Try it yourself
package main
import "fmt"
func main() {
// Source slice with fruit names
source := []string{"apple", "banana", "cherry", "date"}
// Create a destination slice with capacity for 3 elements
destination := make([]string, 3)
// TODO: Use the copy function to copy elements from source to destination
// Print the destination slice
fmt.Println("Destination slice:", destination)
}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