Created
February 14, 2019 10:20
-
-
Save totoleo/10376e629d1f5478e73bfd5569a0d477 to your computer and use it in GitHub Desktop.
a pitfall of slice
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 main() { | |
//case 1 | |
a := []int{} | |
a = append(a, 1) | |
a = append(a, 2) | |
b := append(a, 3) | |
c := append(a, 4) | |
fmt.Println("a: ", a, "\nb: ", b, "\nc: ", c) | |
//case 2 | |
a = append(a, 3) | |
x := append(a, 4) | |
y := append(a, 5) | |
fmt.Println("a: ", a, "\nx: ", x, "\ny: ", y) | |
} | |
/* | |
这是上面程序的输出么 | |
a: [1 2] | |
b: [1 2 3] | |
c: [1 2 4] | |
a: [1 2 3] | |
x: [1 2 3 4] | |
y: [1 2 3 5] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment