Go math.Min
Choose the smaller of the voucher balance and the amount owed.
A simple example
math.Min
- Start with 2 and 5
- Do this Choose the smaller value
- Result 2
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Min(2.0, 5.0))
}
2
Syntax
math.Min(voucher, due)
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goCap a voucher at the total due
A shopper presents store credit at checkout. The till must cap the usable credit at the outstanding bill and handle boundary amounts before it prints the balance.
package main
import (
"fmt"
"math"
)
func main() {
voucher := 12.0
due := 8.0
fmt.Printf("%.3f\n", math.Min(voucher, due))
}
8.000
A voucher worth twelve covers eight on an eight-unit bill.
Limit a reservation to available seats
A booking screen receives a requested party size and the current seat inventory. Use the available capacity to decide how many places can actually be assigned.
package main
import (
"fmt"
"math"
)
func main() {
requested := 18.0
available := 12.0
fmt.Printf("%.3f\n", math.Min(requested, available))
}
12.000
Twelve seats remain, so a request for eighteen can reserve at most twelve.
Limit a workshop booking
A parent requests five seats, but the workshop has only three places left. The booking system chooses the smaller count.
The booking never assigns more seats than the venue has available.
What to watch for
Apply the same currency and unit to both values.