diff --git a/golang_learn/data_func/go_to_channel/Demo01.go b/golang_learn/data_func/go_to_channel/Demo01.go new file mode 100644 index 0000000..614f27c --- /dev/null +++ b/golang_learn/data_func/go_to_channel/Demo01.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +func consumer(ch chan int, done chan string) { + for i := range done { + fmt.Printf("------消息:%s\n", i) + } + done <- "ok" +} +func product(ch chan int) { + for i := 0; i < 5; i++ { + fmt.Printf("生产:%d\n", i) + ch <- i + } + close(ch) +} + +func main() { + fmt.Println("开始测试!") + ch := make(chan int) + done := make(chan string) + go consumer(ch, done) + go product(ch) + + // 为了让主进程等待消费者结束 + <-done // 注释:会导致第一个生成无法调用 + // <-ch // 注释:不影响逻辑 + +} diff --git a/golang_learn/data_learn/learn_bank/Main.go b/golang_learn/data_learn/learn_bank/Main.go new file mode 100644 index 0000000..617b416 --- /dev/null +++ b/golang_learn/data_learn/learn_bank/Main.go @@ -0,0 +1,90 @@ +package main + +import ( + "fmt" +) + +type Account struct { + AccountNo string + Pwd string + Balance float64 +} + +//存款 +func (account *Account) Deposite() { + + var money float64 + fmt.Print("请输入存款金额:") + fmt.Scan(&money) + + if money <= 0 { + fmt.Println("您输入的金额不正确") + return + } + account.Balance += money + fmt.Println("存款", money, "成功,当前金额为:", account.Balance) +} + +//取款 +func (account *Account) WithDraw() { + + var money float64 + fmt.Print("请输入取款金额:") + fmt.Scan(&money) + + if money <= 0 || money > account.Balance { + fmt.Println("您输入的金额不正确") + return + } + account.Balance -= money + fmt.Println("取款", money, "成功,当前金额为:", account.Balance) +} + +//查询 +func (account *Account) query() { + /** + 存储 小数超 8 位时,会存在精度问题: (+1.99999999=3.6799999899999998,%f=3.680000) + 存储 小数超 7 位时,不存在精度问题,但是会省略:(+1.9999999=3.6799999,%f=3.680000) + 存储 小数超 6 位时,不存在精度问题,不会省略: (+1.999999=3.679999,%f=3.679999) + */ + fmt.Printf("你的账号为:%v 余额:%v (补充:%f)\n", account.AccountNo, account.Balance, account.Balance) +} + +func main() { + //测试 + account := Account{ + //AccountNo : "guangda123456", + //Pwd : "111111", + AccountNo: "1", + Pwd: "1", + Balance: 1.68, + } + + var ano, pwd string + fmt.Print("请输入账号:") + fmt.Scanln(&ano) + fmt.Print("请输入和密码:") + fmt.Scanln(&pwd) + if account.AccountNo == ano && account.Pwd == pwd { + for { + var number int + fmt.Print("请选择您想要的操作(1.存款; 2.取款; 3.查询; 4.退出程序):") + fmt.Scanln(&number) + if number == 1 { + account.Deposite() + } + if number == 2 { + account.WithDraw() + } + if number == 3 { + account.query() + } + if number == 4 { + fmt.Println("已退出程序...") + break + } + } + } else { + fmt.Println("用户名或者密码输入有误,请重新输入...") + } +}