Golang コピー スライス

Sheeraz Gul 2023年6月20日
Golang コピー スライス

このチュートリアルでは、GoLang でスライスをコピーする方法を示します。

GoLang でスライスをコピーする

GoLang でのスライスのコピーは、さまざまな方法で実現できます。 copy() および append() メソッドは、通常、この目的のために使用されます。copy() は、特定のスライスのディープ コピーを取得し、append() メソッドは、スライスのコンテンツをコピーします。 空のスライスにスライスします。

Copy() メソッドを使用して Go でスライスをコピーする

copy() メソッドは、スライスのディープ コピーを作成するため、Golang でスライスをコピーするための最良の方法と考えられています。

構文:

func copy(destination, source []Type) int

宛先はコピーされたスライスであり、ソースはコピー元のスライスです。 この関数は、コピーされたスライスの要素数を返します。

コード例:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7", "Delftstack8"}
    DstSlice1 := make([]string, len(DemoSlice1))
    var DemoSlice2 []string
    DstSlice2 := []string{"Tutorials1"}
    DemoSlice3 := []string{"Delftstack9", "Delftstack10"}
    DstSlice3 := []string{"Tutorials1", "Tutorials2"}
    DemoSlice4 := []string{"Delftstack11", "Delftstack13", "Delftstack14", "Delftstack15"}
    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)
}

上記のコードは、異なるメンバーを持つ 4つのソース スライスと 4つの宛先スライスを作成するか、同じ長さの空のスライスを作成します。

出力:

Source Slice1 is : [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
Destination Slice1 is : [       ]
Source Slice2 is : []
Destination Slice2 is : [Tutorials1]
Source Slice3 is : [Delftstack9 Delftstack10]
Destination Slice3 is : [Tutorials1 Tutorials2]
Source Slice4 is : [Delftstack11 Delftstack13 Delftstack14 Delftstack15]
Destination Slice1 is : [Tutorials1 Tutorials2 Tutorials3]

 Destination Slice 1 after copying: [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
Total number of elements copied: 8

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

 Destination Slice 3 after copying: [Delftstack9 Delftstack10]
Total number of elements copied: 2

 Destination Slice 4 after copying: [Delftstack11 Delftstack13 Delftstack14]
Total number of elements copied: 3

Program exited.

Go で Append() メソッドを使用してスライスをコピーする

append メソッドは、指定されたスライスのコンテンツを宛先スライスに追加します。

構文:

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

append メソッドは、ソース スライスのコンテンツを空の宛先スライスまたはメンバーを含むスライスにコピーします。 copy メソッドとは異なり、以前のメンバーとコピーされたメンバーを含むスライスが返されます。

コード例:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7", "Delftstack8"}
    var DstSlice1 []string
    var DemoSlice2 []string
    DstSlice2 := []string{"Tutorials1"}
    DemoSlice3 := []string{"Delftstack9", "Delftstack10"}
    DstSlice3 := []string{"Tutorials1", "Tutorials2"}
    DemoSlice4 := []string{"Delftstack11", "Delftstack13", "Delftstack14", "Delftstack15"}
    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)
}

上記のコード スニペットは、スライスのコンテンツを別のスライスにコピーします。

出力:

Source Slice1 is : [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
Destination Slice1 is : []
Source Slice2 is : []
Destination Slice2 is : [Tutorials1]
Source Slice3 is : [Delftstack9 Delftstack10]
Destination Slice3 is : [Tutorials1 Tutorials2]
Source Slice4 is : [Delftstack11 Delftstack13 Delftstack14 Delftstack15]
Destination Slice1 is : [Tutorials1 Tutorials2 Tutorials3]
The copied slice 1 after copying:  [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
The copied slice 2 after copying:  [Tutorials1]
The copies slice 3 after copying:  [Tutorials1 Tutorials2 Delftstack9 Delftstack10]
The copied slice 4 after copying:  [Tutorials1 Tutorials2 Tutorials3 Delftstack11 Delftstack13 Delftstack14 Delftstack15]

Program exited

割り当てメソッドを使用して Go でスライスをコピーする

割り当て方法 copy は、ソース スライスを宛先スライスに割り当てるスライスの浅いコピーです。

コード例:

package main
import "fmt"

func main() {
    // Create slices
    DemoSlice1 := []string{"Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7", "Delftstack8"}
    var DemoSlice2 []string
    DemoSlice3 := []string{"Delftstack9", "Delftstack10"}
    DemoSlice4 := []string{"Delftstack11", "Delftstack13", "Delftstack14", "Delftstack15"}

    // 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)
}

ご覧のとおり、スライスを他のスライスに割り当てます。 これは浅い方法であり、コピーの内容を変更すると元のスライスも変更されるため、広く使用されていません。

出力:

Source Slice1 is : [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
Destination Slice1 is : [Delftstack1 Delftstack2 Delftstack3 Delftstack4 Delftstack5 Delftstack6 Delftstack7 Delftstack8]
Source Slice2 is : []
Destination Slice2 is : []
Source Slice3 is : [Delftstack9 Delftstack10]
Destination Slice3 is : [Delftstack9 Delftstack10]
Source Slice4 is : [Delftstack11 Delftstack13 Delftstack14 Delftstack15]
Destination Slice1 is : [Delftstack11 Delftstack13 Delftstack14 Delftstack15]

Program exited.
著者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

関連記事 - Go Slice