Golang find smallest number in array

Well, it works.

Join the Conversation

3 Comments

  1. This as well:

    package main

    import (
    “fmt”
    )

    func main() {
    x := []int{
    48, 96, 86, 68,
    57, 82, 63, 70,
    37, 34, 83, 27,
    19, 97, 9, 17,
    }

    small := x[0]; big := x[0]
    for _, value := range x {
    if value > big {
    big = value
    } else if value < small {
    small = value
    }
    }
    fmt.Printf("smallest: %d, biggest: %d\n", small, big)
    }

  2. this is a bit shorter way of doing that
    smallest := x[0]
    for _, num := range x[1:] {
    if num < smallest {
    smallest = num //set the smallest to num
    }
    }
    fmt.Println(smallest)

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.