JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

Golang copy slice

Author:JIYIK Last Updated:2025/04/15 Views:

This article introduces how to copy slices in Go language.


Copying a Slice in GoLang

Copying a slice in Go can be achieved by different methods. The copy()and append()methods are commonly used for this purpose, where copy()takes a deep copy of the given slice, while append()the method copies the contents of a slice into an empty slice.

Copy a slice in Go using Copy() method

copy()method is considered as the best way to copy a slice in Golang as it creates a deep copy of the slice.

grammar:

func copy(destination, source []Type) int

The destination is the slice to copy and the source is the slice we copied from. The function returns the number of elements of the copied slice.

Code example:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Jiyik1", "Jiyik2", "Jiyik3", "Jiyik4", "Jiyik5", "Jiyik6", "Jiyik7", "Jiyik8"}
    DstSlice1 := make([]string, len(DemoSlice1))
    var DemoSlice2 []string
    DstSlice2 := []string{"Tutorials1"}
    DemoSlice3 := []string{"Jiyik9", "Jiyik10"}
    DstSlice3 := []string{"Tutorials1", "Tutorials2"}
    DemoSlice4 := []string{"Jiyik11", "Jiyik13", "Jiyik14", "Jiyik15"}
    DstSlice4 := []string{"Tutorials1", "Tutorials2", "Tutorials3"}

    // Slices Before copying
    fmt.Println("Source Slice1 is :", DemoSlice1)
    fmt.Println("Destination Slice1 is :", DstSlice1)
    fmt.Println("Source Slice2 is :", DemoSlice2)
    fmt.Println("Destination Slice2 is :", DstSlice2)
    fmt.Println("Source Slice3 is :", DemoSlice3)
    fmt.Println("Destination Slice3 is :", DstSlice3)
    fmt.Println("Source Slice4 is :", DemoSlice4)
    fmt.Println("Destination Slice1 is :", DstSlice4)

    // Copy the slices
    CopiedSlice1 := copy(DstSlice1, DemoSlice1)
    fmt.Println("\n Destination Slice 1 after copying:", DstSlice1)
    fmt.Println("Total number of elements copied:", CopiedSlice1)

    CopiedSlice2 := copy(DstSlice2, DemoSlice2)
    fmt.Println("\n Destination Slice 2 after copying:", DstSlice2)
    fmt.Println("Total number of elements copied:", CopiedSlice2)

    CopiedSlice3 := copy(DstSlice3, DemoSlice3)
    fmt.Println("\n Destination Slice 3 after copying:", DstSlice3)
    fmt.Println("Total number of elements copied:", CopiedSlice3)

    CopiedSlice4 := copy(DstSlice4, DemoSlice4)
    fmt.Println("\n Destination Slice 4 after copying:", DstSlice4)
    fmt.Println("Total number of elements copied:", CopiedSlice4)
}

The above code creates four source slices and four destination slices with different members or empty slices with the same length.

Output:

Source Slice1 is : [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
Destination Slice1 is : [       ]
Source Slice2 is : []
Destination Slice2 is : [Tutorials1]
Source Slice3 is : [Jiyik9 Jiyik10]
Destination Slice3 is : [Tutorials1 Tutorials2]
Source Slice4 is : [Jiyik11 Jiyik13 Jiyik14 Jiyik15]
Destination Slice1 is : [Tutorials1 Tutorials2 Tutorials3]

 Destination Slice 1 after copying: [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
Total number of elements copied: 8

 Destination Slice 2 after copying: [Tutorials1]
Total number of elements copied: 0

 Destination Slice 3 after copying: [Jiyik9 Jiyik10]
Total number of elements copied: 2

 Destination Slice 4 after copying: [Jiyik11 Jiyik13 Jiyik14]
Total number of elements copied: 3

Copying a Slice Using Append() Method in Go

appendmethod appends the contents of the given slice to the destination slice.

grammar:

func destination = append(destination, source...)

The append method copies the contents of the source slice to an empty destination slice or a slice with members. It returns a slice containing the previous members and the copied members, unlike the copy method.

Code example:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Jiyik1", "Jiyik2", "Jiyik3", "Jiyik4", "Jiyik5", "Jiyik6", "Jiyik7", "Jiyik8"}
    var DstSlice1 []string
    var DemoSlice2 []string
    DstSlice2 := []string{"Tutorials1"}
    DemoSlice3 := []string{"Jiyik9", "Jiyik10"}
    DstSlice3 := []string{"Tutorials1", "Tutorials2"}
    DemoSlice4 := []string{"Jiyik11", "Jiyik13", "Jiyik14", "Jiyik15"}
    DstSlice4 := []string{"Tutorials1", "Tutorials2", "Tutorials3"}

    // Slices Before copying
    fmt.Println("Source Slice1 is :", DemoSlice1)
    fmt.Println("Destination Slice1 is :", DstSlice1)
    fmt.Println("Source Slice2 is :", DemoSlice2)
    fmt.Println("Destination Slice2 is :", DstSlice2)
    fmt.Println("Source Slice3 is :", DemoSlice3)
    fmt.Println("Destination Slice3 is :", DstSlice3)
    fmt.Println("Source Slice4 is :", DemoSlice4)
    fmt.Println("Destination Slice1 is :", DstSlice4)

    // Copy the slices
    CopiedSlice1 := append(DstSlice1, DemoSlice1...)
    fmt.Println("The copied slice 1 after copying: ", CopiedSlice1)

    CopiedSlice2 := append(DstSlice2, DemoSlice2...)
    fmt.Println("The copied slice 2 after copying: ", CopiedSlice2)

    CopiedSlice3 := append(DstSlice3, DemoSlice3...)
    fmt.Println("The copies slice 3 after copying: ", CopiedSlice3)

    CopiedSlice4 := append(DstSlice4, DemoSlice4...)
    fmt.Println("The copied slice 4 after copying: ", CopiedSlice4)
}

The above code snippet will copy the contents of one slice to another slice.

Output:

Source Slice1 is : [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
Destination Slice1 is : []
Source Slice2 is : []
Destination Slice2 is : [Tutorials1]
Source Slice3 is : [Jiyik9 Jiyik10]
Destination Slice3 is : [Tutorials1 Tutorials2]
Source Slice4 is : [Jiyik11 Jiyik13 Jiyik14 Jiyik15]
Destination Slice1 is : [Tutorials1 Tutorials2 Tutorials3]
The copied slice 1 after copying:  [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
The copied slice 2 after copying:  [Tutorials1]
The copies slice 3 after copying:  [Tutorials1 Tutorials2 Jiyik9 Jiyik10]
The copied slice 4 after copying:  [Tutorials1 Tutorials2 Tutorials3 Jiyik11 Jiyik13 Jiyik14 Jiyik15]

Copying a slice using assignment method in Go

The assignment method copy is a shallow copy of the slice, we assign the source slice to the destination slice.

Code example:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Jiyik1", "Jiyik2", "Jiyik3", "Jiyik4", "Jiyik5", "Jiyik6", "Jiyik7", "Jiyik8"}
    var DemoSlice2 []string
    DemoSlice3 := []string{"Jiyik9", "Jiyik10"}
    DemoSlice4 := []string{"Jiyik11", "Jiyik13", "Jiyik14", "Jiyik15"}

    // Slices After copying
    fmt.Println("Source Slice1 is :", DemoSlice1)
    DstSlice1 := DemoSlice1
    fmt.Println("Destination Slice1 is :", DstSlice1)
    fmt.Println("Source Slice2 is :", DemoSlice2)
    DstSlice2 := DemoSlice2
    fmt.Println("Destination Slice2 is :", DstSlice2)
    fmt.Println("Source Slice3 is :", DemoSlice3)
    DstSlice3 := DemoSlice3
    fmt.Println("Destination Slice3 is :", DstSlice3)
    fmt.Println("Source Slice4 is :", DemoSlice4)
    DstSlice4 := DemoSlice4
    fmt.Println("Destination Slice1 is :", DstSlice4)
}

As we can see, we assign slices to other slices. This is a superficial approach and is not widely used because when we modify the copied content, the original slice also changes.

Output:

Source Slice1 is : [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
Destination Slice1 is : [Jiyik1 Jiyik2 Jiyik3 Jiyik4 Jiyik5 Jiyik6 Jiyik7 Jiyik8]
Source Slice2 is : []
Destination Slice2 is : []
Source Slice3 is : [Jiyik9 Jiyik10]
Destination Slice3 is : [Jiyik9 Jiyik10]
Source Slice4 is : [Jiyik11 Jiyik13 Jiyik14 Jiyik15]
Destination Slice1 is : [Jiyik11 Jiyik13 Jiyik14 Jiyik15]

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Install GoLang using Brew

Publish Date:2025/04/15 Views:82 Category:Go

This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install

GoLang RWMutex Detailed Introduction

Publish Date:2025/04/15 Views:116 Category:Go

This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide

Enabling CORS in GoLang

Publish Date:2025/04/15 Views:102 Category:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

Function pointers in Go

Publish Date:2025/04/15 Views:83 Category:Go

A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na

Duration in Go

Publish Date:2025/04/15 Views:89 Category:Go

Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time. Note that duration refers to the time elapsed between two defined time objects, as an int64

Create a new directory in Go

Publish Date:2025/04/15 Views:112 Category:Go

Go has many ways to create directories, one of which is os.Mkdir . We can assign specified names and permission bits to the newly formed directory. os.MkdirAll Useful for recursively creating multiple directories, including any missing pare

How to read a file into a string in GoLang

Publish Date:2025/04/15 Views:90 Category:Go

The Go language provides a lot of file manipulation tools, one of which is how to read files into strings. ioutil.ReadFile() , File.Read() , buf.ReadFrom() and strings.Builder are just a few of the methods that can be used to efficiently wr

Declaring constant Map in Go

Publish Date:2025/04/15 Views:166 Category:Go

Map is a collection of key-value pairs which can be sorted in any order. It assigns values ​​to keys. The keys in a Map are always unique, but the values ​​are not always unique. The map data structure is used for fast key-based dat

Merge two maps in Golang

Publish Date:2025/04/15 Views:164 Category:Go

This article describes how to merge two maps in Golang. Golang does not provide any in-built functionality to merge mappings, but this can be achieved using Copy method or by following the corresponding procedure. Let’s try both the metho

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial