Golang: the extreme way to obscure through convention

The way to use append in Golang

is described as following:

1
a = append(a, x)

because append may either modify its argument in-place or return a copy of its argument with an additional entry, depending on the size and capacity of its input. Using a slice that was previously appended to may give unexpected results.

Read it carefully

That basically is saying you cannot use array when you use slice. But why would you want to? Mother knows best, stick to slice after all.

Run following code to have your hair streched:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import "fmt"
func main() {
fmt.Println("Hello World")
var s []int
s = make([]int, 5)
fmt.Println(s)
s[3] = 10
fmt.Println(s)
var a = [5]int{1, 2, 3, 4, 5}
fmt.Println(a)
var sa = a[0:4]
fmt.Println(sa)
sa[3] = 15
fmt.Println(a)
fmt.Println(sa)
sa = append(sa, 20)
fmt.Println(a)
fmt.Println(sa)
sa = append(sa, 25)
fmt.Println(a)
fmt.Println(sa)
sa[4] = 30
fmt.Println(a)
fmt.Println(sa)
s2 := make([]int, 6)
copy(s2[1:], sa)
fmt.Println(s2)
}