NASutils Knowledge BaseUnofficialPolski

Go · lesson 12 of 30 · Decisions and loopsKeep values in a slice

append adds, [:2] takes a part, the package slices sorts and searches.

Programming on the NAS › Go in 30 lessons

12-slices.go Download 12-slices.go

package main

import (
	"fmt"
	"slices"
)

func main() {
	temps := []int{38, 39, 53, 44}
	temps = append(temps, 41)

	fmt.Println(len(temps), "values:", temps)
	fmt.Println("first two:", temps[:2])
	fmt.Println("the warmest:", slices.Max(temps))

	sorted := slices.Clone(temps)
	slices.Sort(sorted)
	fmt.Println("sorted:", sorted)
	fmt.Println("as it was:", temps)
	fmt.Println("is 53 there?", slices.Contains(temps, 53))
}

Terminal

$ ./12-slices
5 values: [38 39 53 44 41]
first two: [38 39]
the warmest: 53
sorted: [38 39 41 44 53]
as it was: [38 39 53 44 41]
is 53 there? true

Exercise

Remove every value under 40.

TurboShell does not contain Go. These programs are made in Bucket & Code with its Go runtime; the finished file runs on the NAS without Go.