Menu
Coddy logo textTech

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: 2

The copy function copies only as many elements as will fit in the destination slice.

challenge icon

Challenge

Beginner

In 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)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals