golang存取查简单实例
package main import "fmt" //定义结构体的时候,实际上就是把一类失误的共有属性和行为提取出来,形成一个物理模型 //定义一个Account结构体 type Account struct { AccountNo string Pwd int Balance float64 } //存款 func (account *Account) Deposite(money float64, pwd int) { if pwd != account.Pwd { fmt.Println("您输入的密码不正确") return } if money < 0 { fmt.Println("存入金额不正确") return } account.Balance = account.Balance + money fmt.Println("存款成功") } //取款 func (account *Account) WithMoney(money float64, pwd int) { if pwd != account.Pwd { fmt.Println("您输入的密码不正确") return } if money < 0 || money > account.Balance { fmt.Println("取出金额不正确") return } account.Balance = account.Balance - money fmt.Println("取款成功") } //查询 func (account *Account) Query(pwd int) { if pwd != account.Pwd { fmt.Println("您输入的密码不正确") return } fmt.Println("查询结果为 你的账户余额 ", account.Balance) } func main() { var account = Account{ AccountNo: "1008611", Pwd: 123456, Balance: 10000000.00, } var money float64 var pwd int //存款 fmt.Println("请输入存入金额") fmt.Scanln(&money) fmt.Println("请输入密码") fmt.Scanln(&pwd) account.Deposite(money, pwd) //取款 fmt.Println("请输入取出金额") fmt.Scanln(&money) fmt.Println("请输入密码") fmt.Scanln(&pwd) account.WithMoney(money, pwd) //查询 fmt.Println("查询账户余额") fmt.Println("请输入密码") fmt.Scanln(&pwd) account.Query(pwd) }