Welcome To Golang By Example

Max of two numbers in Go (Golang)

Table of Contents

Overview

math package of GO provides a Max method that can be used to get the maximum of two numbers.

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

func Max(x, y float64) float64

Also some special cases of Max function are

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    max := math.Max(2, 3)
    fmt.Println(max)

    max = math.Max(-2.1, -3.3)
    fmt.Println(max)
}

Output:

3
-2.1