Skip to content

Instantly share code, notes, and snippets.

@draveness
Created April 21, 2020 13:19
Show Gist options
  • Save draveness/f2055f9c03f587fe42c4a45321c84512 to your computer and use it in GitHub Desktop.
Save draveness/f2055f9c03f587fe42c4a45321c84512 to your computer and use it in GitHub Desktop.
Deposit and Withdraw with float64
package main
import (
"fmt"
)
var balance float64 = 0
func main() {
deposit(.1)
deposit(.2)
if balance, ok := withdraw(0.30000000000000004); ok {
fmt.Println(balance)
}
}
func deposit(v float64) {
balance += v
}
func withdraw(v float64) (float64, bool) {
if v <= balance {
balance -= v
return v, true
}
return 0, false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment