Skip to content

Instantly share code, notes, and snippets.

@totoleo
Created February 14, 2019 10:20
Show Gist options
  • Save totoleo/10376e629d1f5478e73bfd5569a0d477 to your computer and use it in GitHub Desktop.
Save totoleo/10376e629d1f5478e73bfd5569a0d477 to your computer and use it in GitHub Desktop.
a pitfall of slice
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