Google News
logo
Golang - Interview Questions
Implement min and max behavior in Golang.
Implement Min(x, y int) and Max(x, y int) functions that take two integers and return the lesser or greater value, respectively.
 
Example :
By default, Go only supports min and max for floats using math.min and math.max. You’ll have to create your own implementations to make it work for integers.
package main

import "fmt"

// Min returns the smaller of x or y.

func Min(x, y int) int {
        if x > y {
                return y
        }
        return x
}

// Max returns the larger of x or y.

func Max(x, y int) int {
        if x < y {
                return y
        }
        return x
}

func main() { 
    fmt.Println(Min(5,10))
    fmt.Println(Max(5,10))
}​

Output :

5
10

 

Advertisement