知识卡片
定义类型让数值带领域语义
type Bitcoin int创建新的定义类型,为整数加上领域身份,并可在同包为它声明方法;这不是type Bitcoin = int形式的类型别名。显式转换让类型意图清楚,但仍沿用int的范围及运算规则,不自动处理精度单位、溢出或金融校验。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 指针和错误
原书出处 · 1 处
- 指针和错误 · 查看原书依据
type Bitcoin int type Wallet struct { balance Bitcoin } func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } func (w *Wallet) Balance() Bi
参考来源
- 位置:《通过测试学习 Go 编程》指针和错误,spine010(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/09.html)。
- 结论依据:真实无等号类型声明,纠正原文混称别名。
- 原始内容:type Bitcoin int type Wallet struct { balance Bitcoin } func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } func (w *Wallet) Balance() Bi