Application
Create new folder named src. In src folder, create new file named main.go as below:
package main
import (
"fmt"
)
func main() {
var a = [5]int{4, 1, -2, 9, 10}
slice1 := a[0:2]
fmt.Println("slice 1")
fmt.Println("\tLength:", len(slice1))
fmt.Println("\tCapacity:", cap(slice1))
slice2 := a[:4]
fmt.Println("slice 2")
fmt.Println("\tLength:", len(slice2))
fmt.Println("\tCapacity:", cap(slice2))
slice3 := a[4:]
fmt.Println("slice 3")
fmt.Println("\tLength:", len(slice3))
fmt.Println("\tCapacity:", cap(slice3))
slice4 := a[:]
fmt.Println("slice 4")
fmt.Println("\tLength:", len(slice4))
fmt.Println("\tCapacity:", cap(slice4))
}
Output
Open Terminal windows in Visual Studio Code and run command line: go run main.go
slice 1
Length: 2
Capacity: 5
slice 2
Length: 4
Capacity: 5
slice 3
Length: 1
Capacity: 1
slice 4
Length: 5
Capacity: 5