# Finding Maximum of Two Numbers in Golang

In programming, we often need to compare two values and find the maximum of the two. In Golang, we have multiple ways to find the maximum of two numbers. In this article, we will discuss different approaches to find the maximum of two numbers in Golang.

## Using if-else Statement

One of the simplest ways to find the maximum of two numbers in Golang is by using an if-else statement. The logic is straightforward: we check if the first number is greater than the second number, and if it is, we assign the first number to the maximum variable; otherwise, we assign the second number to the maximum variable.

### Example

package main

import "fmt"

func max(a, b int) int {
var max int
if a > b {
max = a
} else {
max = b
}
return max
}

func main() {
fmt.Println(max(10, 5))
}


### Output

10


In the above code, we declare a function named max that takes two integer arguments a and b. Inside the function, we declare a variable named max and use an if-else statement to assign the maximum of the two numbers to it. Finally, we return the maximum value.

## Using Math Package

Golang's math package provides a Max function that takes two float64 arguments and returns the maximum value. We can use this function to find the maximum of two numbers. Here is the code −

### Example

package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(max(5, 10))          // Output: 10
fmt.Println(max(3.14, 2.71))     // Output: 3.14
}

func max(a, b float64) float64 {
return math.Max(a, b)
}


### Output

10
3.14


In the above code, we import the math package and declare a function named max that takes two float64 arguments a and b. Inside the function, we use the Max function of the math package to find the maximum value and return it.

## Using Ternary Operator

Golang does not have a ternary operator like other programming languages, but we can use a shorthand if-else statement to achieve the same functionality. Here is the code −

### Example

package main

import (
"fmt"
)

func main() {
a, b := 5, 7
fmt.Printf("Max of %d and %d is %d", a, b, max(a, b))
}

func max(a, b int) int {
max := a
if b > a {
max = b
}
return max
}


### Output

Max of 5 and 7 is 7


In the above code, we declare a function named max that takes two integer arguments a and b. Inside the function, we declare a variable named max and assign the value of a to it. Then, we use a shorthand if-else statement to check if b is greater than a. If it is, we assign the value of b to the max variable. Finally, we return the maximum value.

## Using Slices

Golang's built-in sort package provides a Ints function that takes a slice of integers as an argument and sorts it in ascending order. We can use this function to find the maximum value in a slice of integers. Here is the code −

### Example

package main

import (
"fmt"
"sort"
)

func max(numbers []int) int {
sort.Ints(numbers)
return numbers[len(numbers)-1]
}

func main() {
numbers := []int{3, 6, 2, 8, 1}
max := max(numbers)
fmt.Println(max)
}


### Output

8


In the above code, we import the sort package and declare a function named max that takes a slice of integers as an argument. Inside the function, we use the Ints function of the sort package to sort the slice in ascending order. Then, we return the last element of the sorted slice, which is the maximum value.

## Conclusion

In this article, we discussed different approaches to find the maximum of two numbers in Golang. We used if-else statements, the math package, shorthand if-else statements, and the sort package to find the maximum value.

Updated on: 12-Apr-2023

435 Views 