Application
Create new folder named src. In src folder, create new file named main.go as below:
package main
import (
"fmt"
)
func main() {
slice1 := make([]int, 3)
fmt.Println("Length:", len(slice1))
fmt.Println("Capacity:", cap(slice1))
slice1[0] = 4
slice1[1] = 2
slice1[2] = -4
slice1 = append(slice1, 20)
slice1 = append(slice1, 20, 30, 40, 50)
fmt.Println("slice 1: ", slice1)
slice2 := make([]int, 4, 8)
fmt.Println("Length:", len(slice2))
fmt.Println("Capacity:", cap(slice2))
slice2[0] = 2
slice2[1] = 9
slice2[2] = -8
fmt.Println("slice 2: ", slice2)
}
Output
Open Terminal windows in Visual Studio Code and run command line: go run main.go
Length: 3
Capacity: 3
slice 1: [4 2 -4 20 20 30 40 50]
Length: 4
Capacity: 8
slice 2: [2 9 -8 0]