更多博文尽在专栏go全部教程
关于本博主的全部go博文里边的代码均可直接运行
本篇文章只是很是简单的一个案例,会把代码使用面向对象的思想封装html
案例需求
- 实现一个简单的记帐功能
- 记录收支状况
- 友好的退出
案例所包含的知识点
- 变量的声明方式
- 全局变量,局部变量
- 标准输入与输出
- 字符串格式化
- 无限循环
案例初始代码
代码每一步操做都有注释,能够把代码直接复制运行微信
package main import "fmt" func main() { // 声明一个变量,保存用户输入的值 key := "" // 声明一个变量是否退出循环 loop := true // 定义帐户余额 balance := 1000.0 // 每次收支的金额 money := 0.0 // 每次收支的说明 note := "" // 收支的详情 detaile := "收支\t 帐户金额\t 收支余额\t 说 明" // 定义一个flag来判断是否有收支记录 flag := false // 显示这个主菜单 for { fmt.Println("-------------家庭收支记帐软件-----------------") fmt.Println(" 1.收支明细") fmt.Println(" 2.登记收入") fmt.Println(" 3.登记支出") fmt.Println(" 4.退出软件") fmt.Print("请选择1-4: ") fmt.Scan(&key) switch key { case "1": fmt.Println("-------------当前收支明细记录-----------------") if flag { fmt.Println(detaile) } else { fmt.Println("尚未任何记录,快去记录吧!") } case "2": fmt.Println("本次收支金额:") fmt.Scan(&money) balance += money fmt.Println("本次收支说明") fmt.Scan(¬e) detaile += fmt.Sprintf("\n收入 \t%v \t%v \t%v\n", balance, money, note) flag = true case "3": fmt.Println("本次支出金额:") fmt.Scan(&money) // 对支出金额作一个判断 if money > balance { fmt.Println("你的余额不够了,咔咔") break } // 从余额中减掉 balance -= money fmt.Println("本次支出说明") fmt.Scan(¬e) detaile += fmt.Sprintf("\n支出 \t%v \t%v \t%v\n", balance, money, note) flag = true case "4": fmt.Println("你肯定要退出吗? y/n") choice := "" for { fmt.Scan(&choice) if choice == "y" || choice == "n" { break } fmt.Println("你的输入有误,请从新输入 y/n") } if choice == "y" { loop = false } default: fmt.Println("请输入正确1的选项...") } if loop == false { break } } fmt.Println("你退出了记帐软件") }
演示案例
这就是简单的实现效果
函数
用面向对象的思想来实现
这是项目目录
环境变量配置
oop
封装代码
- 到这一步就很简单了
- 咱们只须要把swithc的代码拿出来便可
- 这里须要注意的是封装的函数名须要大写
- 还有环境变量的配置
- 咋就是属性的调用
- 把源码看清
代码所有封装在utils这个包下
而后在main包引用
this
案例效果
封装原代码
package utils import "fmt" type familyAccount struct { // 声明一个变量,保存用户输入的值 key string // 声明一个变量是否退出循环 loop bool // 定义帐户余额 balance float64 // 每次收支的金额 money float64 // 每次收支的说明 note string // 收支的详情 detaile string // 定义一个flag来判断是否有收支记录 flag bool choice string } /** 使用工厂模式返回一个指针 */ func NewFamily() *familyAccount { return &familyAccount{ // 声明一个变量,保存用户输入的值 key: "", // 声明一个变量是否退出循环 loop: true, // 定义帐户余额 balance: 1000.0, // 每次收支的金额 money: 0.0, // 每次收支的说明 note: "", // 收支的详情 detaile: "收支\t 帐户金额\t 收支余额\t 说 明", // 定义一个flag来判断是否有收支记录 flag: false, choice: "", } } /** 显示详情 */ func (this *familyAccount) detail() { fmt.Println("-------------当前收支明细记录-----------------") if this.flag { fmt.Println(this.detaile) } else { fmt.Println("尚未任何记录,快去记录吧!") } } /** 收入 */ func (this *familyAccount) income() { fmt.Println("本次收支金额:") fmt.Scan(&this.money) this.balance += this.money fmt.Println("本次收支说明") fmt.Scan(&this.note) this.detaile += fmt.Sprintf("\n收入 \t%v \t%v \t%v\n", this.balance, this.money, this.note) this.flag = true } /** 支出 */ func (this *familyAccount) expenditure() { fmt.Println("本次支出金额:") fmt.Scan(&this.money) // 对支出金额作一个判断 if this.money > this.balance { fmt.Println("你的余额不够了,咔咔") return } // 从余额中减掉 this.balance -= this.money fmt.Println("本次支出说明") fmt.Scan(&this.note) this.detaile += fmt.Sprintf("\n支出 \t%v \t%v \t%v\n", this.balance, this.money, this.note) this.flag = true } /** 退出 */ func (this *familyAccount) exit() { fmt.Println("你肯定要退出吗? y/n") this.choice = "" for { fmt.Scan(&this.choice) if this.choice == "y" || this.choice == "n" { break } fmt.Println("你的输入有误,请从新输入 y/n") } if this.choice == "y" { this.loop = false } } /** 显示菜单 */ func (this *familyAccount) ShowMenu() { for { fmt.Println("-------------家庭收支记帐软件-----------------") fmt.Println(" 1.收支明细") fmt.Println(" 2.登记收入") fmt.Println(" 3.登记支出") fmt.Println(" 4.退出软件") fmt.Print("请选择1-4: ") fmt.Scan(&this.key) switch this.key { case "1": this.detail() case "2": this.income() case "3": this.expenditure() case "4": this.exit() default: fmt.Println("请输入正确1的选项...") } if this.loop == false { break } } }
调用源码
package main import ( "fmt" "go_code/family/utils" ) func main() { fmt.Println("面向对象的记帐功能") utils.NewFamily().ShowMenu() }