Last active
August 8, 2021 07:12
-
-
Save kben19/f58cd7d349a79d54ae21c7878cf0b81b to your computer and use it in GitHub Desktop.
Wallet example on defer evaluated param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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