August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this subsection, you will learn how to append an existing array to an existing slice using the technique found in appendArrayToSlice.go. The program will be presented in two parts. The first part is the following:
package main
import (
"fmt"
)
func main() {
s := []int{1, 2, 3}
a := [3]int{4, 5, 6}
So far, we have just created and initialized a slice named s and an array named a.
The second part of appendArrayToSlice.go is as follows:
ref := a[:]
fmt.Println("Existing array:\t", ref)
t := append(s, ref...)
fmt.Println("New slice:\t", t)
s = append(s, ref...)
fmt.Println("Existing slice:\t", s)
s = append(s, s...)
fmt.Println("s+s:\t\t", s)
}
Two important things are happening here. The first is that we create ...
Read now
Unlock full access