Start learning free

LESSON 269 OF 671 · SORTING & BUILT-INS

Min, Max and Sum in Go: slices.Min, slices.Max and a Loop

Lesson Read the full explanation

TL ; DR:

slices.Min(s) gives the smallest value of a slice.

slices.Max(s) gives the largest value.

Go has no sum function, so a loop adds the values up.


prices := []int{4, 9, 2, 7}

fmt.Println(slices.Min(prices))    // 2
fmt.Println(slices.Max(prices))    // 9

total := 0

for _, p := range prices {
    total += p
}

fmt.Println(total)                 // 22


Full lesson:

In Maximum Number In Array, a loop found the largest value. Go 1.21 added that loop to the slices package. You used that package for slices.Contains.

  • slices.Min(s) gives the smallest value of the slice s.
  • slices.Max(s) gives the largest value.

The max(a, b) from Maximum Number In Array compares separate values. slices.Max looks at a whole slice.

Go has no function that adds up a slice. Write the loop from Sum of Numbers. Start total at 0, then add each value.

Watch out for an empty slice. slices.Min and slices.Max panic on it, and the program stops. An empty slice has no smallest value. Check len(s) > 0 first. The sum loop just leaves total at 0.


Your task

minutes holds how many minutes you practiced coding on each day of a week.

Store the shortest day in shortest and the longest day in longest. Store the week's total in total. Go has no sum function, so add the values up with a loop.

Use the built-ins from this lesson. The code at the bottom prints all three.

Expected output:

shortest = 15
longest = 60
total = 235
STEP 1 OF 5
    Ctrl + Enter to check

    GO · SORTING & BUILT-INS

    Min Max Sum in Go, explained

    What Go gives you

    Finding the smallest or the largest value of a slice is a loop you can write by hand: keep the best value so far and compare each new one. Since Go 1.21 the standard slices package does it for you:

    • slices.Min(s) returns the smallest value of the slice s.
    • slices.Max(s) returns the largest value.

    Don't confuse them with the built-in min(a, b) and max(a, b), which compare separate values. slices.Max looks at a whole slice at once.

    Go has no function that adds up a slice. For a total you write the loop: start a variable at 0 and add each value to it.

    Examples

    prices := []int{4, 9, 2, 7}
    
    fmt.Println(slices.Min(prices))
    fmt.Println(slices.Max(prices))
    
    total := 0
    for _, p := range prices {
        total += p
    }
    fmt.Println(total)

    This prints:

    2
    9
    22

    In the loop, range gives an index and a value for each position. The underscore throws the index away, since an unused variable is a compile error in Go. In a file of your own, add import "slices" alongside fmt.

    Other element types

    The two functions work on any slice whose items can be compared with <, not only int. With []string{"Rex", "Bo", "Luna"}, slices.Min gives Bo and slices.Max gives Rex, because strings compare in dictionary order. With []float64{3.5, -1.25, 8} they give -1.25 and 8. A slice of structs does not work this way, since structs have no <.

    Watch out for an empty slice

    slices.Min and slices.Max panic on an empty slice, and the program stops. An empty slice has no smallest value, so there is nothing sensible to return. With a nil or empty slice empty, the call slices.Min(empty) panics with the message slices.Min: empty list. Check len(s) > 0 before calling them when the slice might be empty.

    The sum loop needs no such check: over an empty slice it never runs, and total stays at 0, which is the right sum of nothing.

    The lesson's task

    minutes holds how many minutes you practiced on each day of a week: []int{25, 40, 15, 60, 30, 45, 20}. The task is to store the shortest day in shortest, the longest in longest, and the week's total in total. The code at the bottom prints all three.

    1. Declare shortest with := and slices.Min(minutes).
    2. Declare longest the same way with slices.Max(minutes).
    3. Create total and start it at 0.
    4. Loop over minutes with for _, m := range minutes.
    5. Inside the loop, add m to total.
    minutes := []int{25, 40, 15, 60, 30, 45, 20}
    
    shortest := slices.Min(minutes)
    longest := slices.Max(minutes)
    total := 0
    for _, m := range minutes {
        total += m
    }
    
    fmt.Println("shortest =", shortest)
    fmt.Println("longest =", longest)
    fmt.Println("total =", total)

    Its output is:

    shortest = 15
    longest = 60
    total = 235

    The total is 25 + 40 + 15 + 60 + 30 + 45 + 20 = 235.

    Common mistakes

    • Calling slices.Sum. It doesn't exist. Use the loop.
    • Starting total at the first value and then looping over the whole slice. The first value is counted twice.
    • Writing total = 0 without declaring it. Use total := 0 the first time.
    • Passing separate numbers. slices.Min(3, 5) is a compile error; it takes one slice.

    Key takeaways

    • slices.Min(s) and slices.Max(s) replace a hand-written loop for the extremes.
    • Both panic on an empty slice, so check the length when the slice may be empty.
    • Go has no sum function: add the values in a loop that starts at 0.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free