Welcome To Golang By Example

Min of two numbers in Go (Golang)

Table of Contents

Overview

math package of GO provides a Min method that can be used to get the minimum of two numbers. The


Below is the signature of the function. It takes input two float numbers and returns a float.

func Min(x, y float64) float64

Also some special cases of Min function are

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    min := math.Min(2, 3)
    fmt.Println(min)

    min = math.Min(-2.1, -3.3)
    fmt.Println(min)
}

Output:

2
-3.3