Skip to content

Instantly share code, notes, and snippets.

@kben19
Last active August 8, 2021 07:12
Show Gist options
  • Save kben19/f58cd7d349a79d54ae21c7878cf0b81b to your computer and use it in GitHub Desktop.
Save kben19/f58cd7d349a79d54ae21c7878cf0b81b to your computer and use it in GitHub Desktop.
Wallet example on defer evaluated param
type Wallet struct {
money int
}
func (wallet *Wallet) MyMoney() {
fmt.Println("$", wallet.money)
}
func (wallet *Wallet) AddMoney(money int) {
wallet.money += money
}
func (wallet *Wallet) DeductMoney(money int) {
wallet.money -= money
}
func main() {
myWallet := Wallet{1000}
myWallet.MyMoney()
myWallet.AddMoney(100)
defer func() {
myWallet.MyMoney()
}()
myWallet.DeductMoney(600)
defer myWallet.MyMoney()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment