Created
February 9, 2018 10:09
-
-
Save kawakami-o3/77c7eb33356befc678a69e3fc98ff7db to your computer and use it in GitHub Desktop.
golang slice copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func copyNg() { | |
| src := []int{0, 1, 2, 3, 4} | |
| dest := src[0:1] | |
| dest = append(dest, src[4:5]...) | |
| fmt.Println(src) // => [0 4 2 3 4] | |
| } | |
| func copyOk() { | |
| src := []int{0, 1, 2, 3, 4} | |
| dest := make([]int, 0) | |
| dest = append(dest, src[0:1]...) | |
| dest = append(dest, src[4:5]...) | |
| fmt.Println(src) // => [0 1 2 3 4] | |
| } | |
| func main() { | |
| copyNg() | |
| copyOk() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment