Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Max of two numbers in Go (Golang)

Posted on March 28, 2023March 28, 2023 by admin

Table of Contents

  • Overview
  • Code:

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

  • Max(x, +Inf) = Max(+Inf, x) = +Inf
  • Max(x, NaN) = Max(NaN, x) = NaN
  • Max(+0, ±0) = Max(±0, +0) = +0
  • Max(-0, -0) = -0

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
  • go
  • golang
  • math
  • max
  • maximum
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP