Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Min of two numbers in Go (Golang)

Posted on March 28, 2020March 28, 2020 by admin

Table of Contents

  • Overview
  • Code:

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

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

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
  • go
  • golang
  • math
  • min
  • minimum
  • Follow @golangbyexample

    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

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme