Home »
Golang
Go Copy a Slice
Last Updated : May 05, 2025
In Go, you can copy one slice to another using the built-in copy()
function. The copy()
function allows you to copy elements from one slice to another.
Syntax
The syntax for copying a slice is:
n := copy(destination, source)
Here, destination
is the slice where elements will be copied to, and source
is the slice from which elements will be copied. The copy()
function returns the number of elements copied.
Basic Example of Copying a Slice
In this example, we copy one slice into another:
Example
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai"}
var newCities []string
newCities = make([]string, len(cities))
n := copy(newCities, cities) // Copy elements from cities to newCities
fmt.Println("Original cities:", cities)
fmt.Println("Copied cities:", newCities)
fmt.Println("Number of elements copied:", n)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai]
Copied cities: [Delhi Mumbai Chennai]
Number of elements copied: 3
Copying Partial Slices
You can also copy a part of a slice to another slice. The copy()
function will copy as many elements as the destination slice can hold, which may be less than the length of the source slice.
Example
In this example, we copy only part of the original slice:
package main
import "fmt"
func main() {
countries := []string{"India", "USA", "UK", "Australia", "Canada"}
topCountries := make([]string, 3)
n := copy(topCountries, countries) // Copy first 3 elements from countries
fmt.Println("Original countries:", countries)
fmt.Println("Top countries:", topCountries)
fmt.Println("Number of elements copied:", n)
}
When executed, this program outputs:
Original countries: [India USA UK Australia Canada]
Top countries: [India USA UK]
Number of elements copied: 3
What Happens When the Destination is Smaller?
If the destination slice has fewer elements than the source slice, only as many elements as the destination can hold are copied. If the destination slice is larger, the remaining elements in the destination slice are left unchanged.
Example
In this example, the destination slice is smaller than the source slice:
package main
import "fmt"
func main() {
animals := []string{"Tiger", "Lion", "Elephant", "Leopard"}
smallSlice := make([]string, 2)
n := copy(smallSlice, animals) // Copy only the first 2 elements
fmt.Println("Original animals:", animals)
fmt.Println("Small slice:", smallSlice)
fmt.Println("Number of elements copied:", n)
}
When executed, this program outputs:
Original animals: [Tiger Lion Elephant Leopard]
Small slice: [Tiger Lion]
Number of elements copied: 2
Copying a Slice to Itself
It is important to be careful when copying a slice to itself. Go allows this, but the result may be unexpected because the slices will share the same underlying array.
Example
In this example, we copy a slice to itself:
package main
import "fmt"
func main() {
fruits := []string{"Apple", "Banana", "Mango"}
// Copying the slice to itself
n := copy(fruits, fruits)
fmt.Println("Fruits after copying to itself:", fruits)
fmt.Println("Number of elements copied:", n)
}
When executed, this program outputs:
Fruits after copying to itself: [Apple Banana Mango]
Number of elements copied: 3
Copy Slice Elements Using a Loop
Instead of using the built-in copy()
function, you can manually iterate through the source slice and copy the elements to the destination slice using a for
loop.
Example
In this example, we manually iterate through the source slice and append each element to the destination slice using a loop.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai"}
var newCities []string
for i := 0; i < len(cities); i++ {
newCities = append(newCities, cities[i]) // Manually copy each element
}
fmt.Println("Original cities:", cities)
fmt.Println("Copied cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai]
Copied cities: [Delhi Mumbai Chennai]
Copy Slice Using a for range
Loop
Alternatively, you can use the for range
loop, which gives you both the index and the value of the elements, to copy a slice efficiently.
Example
In this example, we use the for range
loop to iterate over the source slice and append each element to the destination slice.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai"}
newCities := []string{}
for _, city := range cities {
newCities = append(newCities, city) // Copy using for range
}
fmt.Println("Original cities:", cities)
fmt.Println("Copied cities:", newCities)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai]
Copied cities: [Delhi Mumbai Chennai]
Copying Slice with Capacity Control
Sometimes you might want to control the capacity of the destination slice when copying. You can use the make()
function to set a larger capacity.
Example
In this example, we specify the capacity of the destination slice when copying, which helps in optimizing memory usage.
package main
import "fmt"
func main() {
cities := []string{"Delhi", "Mumbai", "Chennai", "Bengaluru"}
newCities := make([]string, 3, 5) // Setting the capacity to 5
n := copy(newCities, cities) // Copy the entire slice
fmt.Println("Original cities:", cities)
fmt.Println("Copied cities with capacity:", newCities)
fmt.Println("Number of elements copied:", n)
}
When executed, this program outputs:
Original cities: [Delhi Mumbai Chennai Bengaluru]
Copied cities with capacity: [Delhi Mumbai Chennai]
Number of elements copied: 4
Exercise
Select the correct option to complete each statement about copying a slice in Go.
- To copy elements from one slice s1 to another slice s2 in Go, the function to use is ___.
- If the destination slice s2 is smaller than the source slice s1, then ___ elements are copied.
- If the destination slice s2 is larger than the source slice s1, then the remaining elements of s2 are ___ after the copy operation.
Advertisement
Advertisement