golang err:Today is Thursday,please give me 50$ i go to eat KFC

作者:
淡白
创建时间:
Jul 28, 2022, 12:40:01 PM
Go 奇技淫巧 kfc 59

摘要:今天是星期四。

今天写代码遇到一个奇怪的报错记录一下,对了今天是星期四

package main

var kfc = KFC{}

type KFC struct {
}
type people struct {
	money int
}

func (k *KFC) Buy(p *people) bool {
	temp := p.money
	temp -= 50
	if temp < 0 {
		panic("Today is Thursday,please give me 50$ i go to eat KFC")
	}
	p.money = temp
	return true
}

func main() {
	p := &people{money: 30}
	println(kfc.Buy(p))
}

run:

panic: Today is Thursday,please give me 50$ i go to eat KFC

goroutine 1 [running]:
main.(*KFC).Buy(...)
        /Users/cctc/code/Test/main.go:15
main.main()
        /Users/cctc/code/Test/main.go:23 +0x27


想了半天,最后发现是我钱不够。那么找一个有人的人让他给你50块就行了

package main

var kfc = KFC{}

type KFC struct {
}
type people struct {
	money int
}
func (p *people) give(p2 *people, num int) {
	p.money -= num
	p2.money += num
}
func (k *KFC) Buy(p *people) bool {
	temp := p.money
	temp -= 50
	if temp < 0 {
		panic("Today is Thursday,please give me 50$ i go to eat KFC")
	}
	p.money = temp
	return true
}

func main() {
	p := &people{money: 30}
	p2 := &people{money: 100}
	p2.give(p, 50)
	println(kfc.Buy(p))
	println(kfc.Buy(p2))
}

这样就没问题了。

懂我意思吧