Arrays and Slices
Slices: make, append, range and finding the maximum.
The main structure for storing sequences in Go is the slice: a variable-length array.
• make([]int, n) — create a slice of n zeros;
• append(a, x) — add an element to the end (it returns a new slice!);
• len(a) — the length;
• indices from 0 to len(a)-1.
For traversal there is range — it yields the index and the value of each element.
Go
Enter: 5, then 1 3 5 2 4
The program prints the slice in reverse order.
Finding the maximum is the basic pattern: take the first element and update the maximum in a loop. Range is handy here: write for _, x := range a — the underscore means "the index is not needed".
Go
Finding the maximum. Enter: 5, then 1 3 5 2 4
Task: find the minimum of the same slice and print the minimum and maximum separated by a space.
Then solve the attached problem "Maximum in an Array" and mark the lesson as completed.