- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
Wallet(钱包)
结构体,我们可以利用它存放 Bitcoin(比特币)
func TestWallet(t * testing.T) { wallet : = Wallet{} wallet.Deposit( 10 ) got : = wallet.Balance() want : = 10 if got != want { t.Errorf( " got %d want %d " , got, want) } }
在前面的示例中,我们直接使用字段名称访问字段,但是在 非常强调安全性的钱包 中,我们不想暴露自己的内部状态,而是通过方法来控制访问的权限.
./wallet_test.go:7:12: undefined: Wallet 。
为测试的运行编写最少量的代码并检查失败测试的输出 。
编译器不知道 Wallet 是什么,所以让我们告诉它.
type Wallet struct { }
现在我们已经生成了自己的钱包,尝试再次运行测试 。
./wallet_test.go:9:8: wallet.Deposit undefined (type Wallet has no field or method Deposit) ./wallet_test.go:11:15: wallet.Balance undefined (type Wallet has no field or method Balance) 。
func (w Wallet) Deposit(amount int ) { } func (w Wallet) Balance() int { return 0 }
测试现在应该编译通过了,然后运行 。
wallet_test.go:15: got 0 want 10 。
编写足够的代码使其通过 。
结构中需要一些 balance(余额) 变量来存储状态 。
type Wallet struct { balance int }
balance
func (w Wallet) Deposit(amount int ) { w.balance += amount } func (w Wallet) Balance() int { return w.balance }
现在我们的事业在金融科技的保护下,将会运行并轻松的通过测试 。
wallet_test.go:15: got 0 want 10 。
。
这让人很困惑,我们的代码看上去没问题,我们在余额中添加了新的金额,然后余额的方法应该返回它当前的状态值.
在 Go 中, 当调用一个函数或方法时,参数会被复制 .
当调用 func (w Wallet) Deposit(amount int) 时, w 是来自我们调用方法的副本.
不需要太过计算机化,当你创建一个值,例如一个 wallet ,它就会被存储在内存的某处。你可以用 &myval 找到那块内存的 地址 .
通过在代码中添加一些 prints 来试验一下 。
func TestWallet(t * testing.T) { wallet : = Wallet{} wallet.Deposit( 10 ) got : = wallet.Balance() fmt.Println( " address of balance in test is " , & wallet.balance) want : = 10 if got != want { t.Errorf( " got %d want %d " , got, want) } }
func (w Wallet) Deposit(amount int ) { fmt.Println( " address of balance in Deposit is " , & w.balance) w.balance += amount }
现在重新运行测试 。
balance
的地址是不同的。
balance
的值时,我们处理的是来自测试的副本。因此, balance
在测试中没有被改变。
func (w *Wallet) Deposit(amount int ) { w.balance += amount } func (w *Wallet) Balance() int { return w.balance }
*Wallet
而不是 Wallet
,你可以将其解读为「指向 wallet
的指针」。
我们曾说过我们正在制做一个比特币钱包,但到目前为止我们还没有提到它们。我们一直在使用 int ,因为当用来计数时它是不错的类型! 。
为此创建一个结构体似乎有点过头了。就 int 的表现来说已经很好了,但问题是它不具有描述性.
Go 允许从现有的类型创建新的类型.
语法是 type MyName OriginalType 。
type Bitcoin int type Wallet struct { balance Bitcoin } func (w * Wallet) Deposit(amount Bitcoin) { w.balance += amount } func (w * Wallet) Balance() Bitcoin { return w.balance }
func TestWallet(t * testing.T) { wallet : = Wallet{} wallet.Deposit(Bitcoin( 10 )) got : = wallet.Balance() want : = Bitcoin( 10 ) if got != want { t.Errorf( " got %d want %d " , got, want) } }
要生成 Bitcoin(比特币) ,你只需要用 Bitcoin(999) 的语法就可以了.
。
类型别名有一个有趣的特性,你还可以对它们声明 方法 。当你希望在现有类型之上添加一些领域内特定的功能时,这将非常有用.
。
type Stringer interface { String() string }
这个接口是在 fmt 包中定义的。当使用 %s 打印格式化的字符串时,你可以定义此类型的打印方式.
func (b Bitcoin) String() string { return fmt.Sprintf( " %d BTC " , b) }
如你所见,在类型别名上创建方法的语法与结构上的语法相同.
接下来,我们需要更新测试中的格式化字符串,以便它们将使用 String() 方法.
if got != want { t.Errorf( " got %s want %s " , got, want) }
为了看到这一点,故意令测试失败我们就能看到 。
wallet_test.go:18: got 10 BTC want 20 BTC 。
。
这使得我们的测试更加清晰.
。
下一个需求是 Withdraw(提取) 函数.
几乎跟 Deposit() 相反 。
func TestWallet(t * testing.T) { t.Run( " Deposit " , func (t * testing.T) { wallet : = Wallet{} wallet.Deposit(Bitcoin( 10 )) got : = wallet.Balance() want : = Bitcoin( 10 ) if got != want { t.Errorf( " got %s want %s " , got, want) } }) t.Run( " Withdraw " , func (t * testing.T) { wallet : = Wallet{balance: Bitcoin( 20 )} wallet.Withdraw( 10 ) got : = wallet.Balance() want : = Bitcoin( 10 ) if got != want { t.Errorf( " got %s want %s " , got, want) } }) }
./wallet_test.go:26:9: wallet.Withdraw undefined (type Wallet has no field or method Withdraw) 。
为测试的运行编写最少量的代码并检查失败测试的输出 。
func (w * Wallet) Withdraw(amount Bitcoin) { }
wallet_test.go:33: got 20 BTC want 10 BTC 。
编写足够的代码使其通过 。
func (w * Wallet) Withdraw(amount Bitcoin) { w.balance -= amount }
func TestWallet(t * testing.T) { assertBalance : = func (t * testing.T, wallet Wallet, want Bitcoin) { got : = wallet.Balance() if got != want { t.Errorf( " got %s want %s " , got, want) } } t.Run( " Deposit " , func (t * testing.T) { wallet : = Wallet{} wallet.Deposit(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) }) t.Run( " Withdraw " , func (t * testing.T) { wallet : = Wallet{balance: Bitcoin( 20 )} wallet.Withdraw(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) }) }
如果你试图从账户中取出更多的钱,会发生什么?目前,我们的要求是假定没有透支设备.
我们如何在使用 Withdraw 时标记出现的问题呢?
在 Go 中,如果你想指出一个错误,通常你的函数要返回一个 err ,以便调用者检查并执行相应操作.
让我们在测试中试试.
。
t.Run( " Withdraw insufficient funds " , func (t * testing.T) { startingBalance : = Bitcoin( 20 ) wallet : = Wallet{startingBalance} err : = wallet.Withdraw(Bitcoin( 100 )) assertBalance(t, wallet, startingBalance) if err == nil { t.Error( " wanted an error but didn't get one " ) } })
如果 你尝试取出超过你余额的比特币,我们想让 Withdraw 返回一个错误,而余额应该保持不变.
然后,如果测试失败,我们检查错误是否为 nil 。
nil 是其他编程语言的 null 。错误可以是 nil ,因为返回类型是 error ,这是一个接口.
如果你看到一个函数,它接受参数或返回值的类型是接口,它们就可以是 nil .
如果你尝试访问一个值为 nil 的值,它将会引发 运行时的 panic 。这很糟糕!你应该确保你检查了 nil 的值.
。
./wallet_test.go:31:25: wallet.Withdraw(Bitcoin(100)) used as value 。
错误信息可能不太清楚,但我们之前对于 Withdraw 的意图只是调用它,它永远不会返回一个值.
为了使它编译通过,我们需要更改它,以便它有一个返回类型.
。
为测试的运行编写最少量的代码并检查失败测试的输出 。
func (w *Wallet) Withdraw(amount Bitcoin) error { w.balance -= amount return nil }
再次强调,编写足够的代码来满足编译器的要求是非常重要的.
我们纠正了自己的 Withdraw 方法返回 error ,现在我们必须返回 一些东西 ,所以我们就返回 nil 好了.
编写足够的代码使其通过 。
func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { return errors.New( " oh no " ) } w.balance -= amount return nil }
记住要将 errors 导入到代码中.
errors.New 创建了一个新的 error ,并带有你选择的消息.
。
。
让我们为错误检查做一个快速测试的助手方法,以帮助我们的测试读起来更清晰.
。
assertError := func (t *testing.T, err error ) { if err == nil { t.Error( " wanted an error but didnt get one " ) } }
并且在我们的测试中 。
t.Run( " Withdraw insufficient funds " , func (t * testing.T) { wallet : = Wallet{Bitcoin( 20 )} err : = wallet.Withdraw(Bitcoin( 100 )) assertBalance(t, wallet, Bitcoin( 20 )) assertError(t, err) })
希望在返回「oh no」的错误时,你会认为我们可能会迭代这个问题,因为它作为返回值看起来没什么用.
假设错误最终会返回给用户,让我们更新测试以断言某种错误消息,而不只是让错误存在.
。
更新一个 string 的助手方法来比较.
assertError := func (t *testing.T, got error , want string ) { if got == nil { t.Fatal( " didn't get an error but wanted one " ) } if got.Error() != want { t.Errorf( " got '%s', want '%s' " , got, want) } }
同时再更新调用者 。
t.Run( " Withdraw insufficient funds " , func (t * testing.T) { startingBalance : = Bitcoin( 20 ) wallet : = Wallet{startingBalance} err : = wallet.Withdraw(Bitcoin( 100 )) assertBalance(t, wallet, startingBalance) assertError(t, err, " cannot withdraw, insufficient funds " ) })
我们已经介绍了 t.Fatal 。如果它被调用,它将停止测试。这是因为我们不希望对返回的错误进行更多断言.
如果没有这个,测试将继续进行下一步,并且因为一个空指针而引起 panic 。
wallet_test.go:61: got err 'oh no' want 'cannot withdraw, insufficient funds' 。
编写足够的代码使其通过 。
func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { return errors.New( " cannot withdraw, insufficient funds " ) } w.balance -= amount return nil }
我们在测试代码和 Withdraw 代码中都有重复的错误消息 。
如果有人想要重新定义这个错误,那么测试就会失败,这将是非常恼人的,而对于我们的测试来说,这里有太多的细节了.
我们并不关心具体的措辞是什么,只是在给定条件的情况下返回一些有意义的错误.
在 Go 中,错误是值,因此我们可以将其重构为一个变量,并为其提供一个单一的事实来源.
var InsufficientFundsError = errors.New( " cannot withdraw, insufficient funds " ) func (w *Wallet) Withdraw(amount Bitcoin) error { if amount > w.balance { return InsufficientFundsError } w.balance -= amount return nil }
var
关键字允许我们定义包的全局值。
Withdraw
函数看起来很清晰。
func TestWallet(t * testing.T) { t.Run( " Deposit " , func (t * testing.T) { wallet : = Wallet{} wallet.Deposit(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) }) t.Run( " Withdraw with funds " , func (t * testing.T) { wallet : = Wallet{Bitcoin( 20 )} wallet.Withdraw(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) }) t.Run( " Withdraw insufficient funds " , func (t * testing.T) { wallet : = Wallet{Bitcoin( 20 )} err : = wallet.Withdraw(Bitcoin( 100 )) assertBalance(t, wallet, Bitcoin( 20 )) assertError(t, err, InsufficientFundsError) }) } func assertBalance(t * testing.T, wallet Wallet, want Bitcoin) { got : = wallet.Balance() if got != want { t.Errorf( " got '%s' want '%s' " , got, want) } } func assertError(t *testing.T, got error , want error ) { if got == nil { t.Fatal( " didn't get an error but wanted one " ) } if got != want { t.Errorf( " got '%s', want '%s' " , got, want) } }
InsufficientFundsError 进行相等的检查,并采取相应的操作
虽然 Go 编译器对你有很大帮助,但有时你仍然会忽略一些事情,错误处理有时会很棘手.
有一种情况我们还没有测试过。要找到它,在一个终端中运行以下命令来安装 errcheck ,这是许多可用的 linters(代码检测工具)之一.
go get -u github.com/kisielk/errcheck 。
然后,在你的代码目录中运行 errcheck . 。
你应该会得到如下类似的内容:
。
wallet_test.go:17:18: wallet.Withdraw(Bitcoin(10)) 。
这告诉我们的是,我们没有检查在代码行中返回的错误.
我的计算机上的这行代码与我们的正常 withdraw 的场景相对应,因为我们没有检查 Withdraw 是否成功,因此没有返回错误 。
。
func TestWallet(t * testing.T) { t.Run( " Deposit " , func (t * testing.T) { wallet : = Wallet{} wallet.Deposit(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) }) t.Run( " Withdraw with funds " , func (t * testing.T) { wallet : = Wallet{Bitcoin( 20 )} err : = wallet.Withdraw(Bitcoin( 10 )) assertBalance(t, wallet, Bitcoin( 10 )) assertNoError(t, err) }) t.Run( " Withdraw insufficient funds " , func (t * testing.T) { wallet : = Wallet{Bitcoin( 20 )} err : = wallet.Withdraw(Bitcoin( 100 )) assertBalance(t, wallet, Bitcoin( 20 )) assertError(t, err, InsufficientFundsError) }) } func assertBalance(t * testing.T, wallet Wallet, want Bitcoin) { got : = wallet.Balance() if got != want { t.Errorf( " got %s want %s " , got, want) } } func assertNoError(t *testing.T, got error ) { if got != nil { t.Fatal( " got an error but didnt want one " ) } } func assertError(t *testing.T, got error , want error ) { if got == nil { t.Fatal( " didn't get an error but wanted one " ) } if got != want { t.Errorf( " got %s, want %s " , got, want) } }
指针 。
。
指针和错误是 Go 开发中重要的组成部分,你需要适应这些。幸运的是,如果你做错了,编译器通常会帮你解决问题,你只需要花点时间读一下错误信息.
最后此篇关于Go语言:如何利用好TDD学习指针并了解Golang中的error处理的文章就讲到这里了,如果你想了解更多关于Go语言:如何利用好TDD学习指针并了解Golang中的error处理的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
对于 Metal ,如果对主纹理进行 mipmap 处理,是否还需要对多采样纹理进行 mipmap 处理?我阅读了苹果文档,但没有得到任何相关信息。 最佳答案 Mipmapping 适用于您将从中
我正在使用的代码在后端 Groovy 代码中具有呈现 GSP(Groovy 服务器页面)的 Controller 。对于前端,我们使用 React-router v4 来处理路由。我遇到的问题是,通过
我们正在 build 一个巨大的网站。我们正在考虑是在服务器端(ASP .Net)还是在客户端进行 HTML 处理。 例如,我们有 HTML 文件,其作用类似于用于生成选项卡的模板。服务器端获取 HT
我正在尝试将图像加载到 void setup() 中的数组中,但是当我这样做时出现此错误:“类型不匹配,'processing .core.PImage' does not匹配“processing.
我正在尝试使用其私有(private)应用程序更新 Shopify 上的客户标签。我用 postman 尝试过,一切正常,但通过 AJAX,它带我成功回调而不是错误,但成功后我得到了身份验证链接,而不
如何更改我的 Processing appIconTest.exe 导出的默认图标在窗口中的应用程序? 默认一个: 最佳答案 经过一些研究,我能找到的最简单的解决方案是: 进入 ...\process
我在 Processing 中做了一个简单的小游戏,但需要一些帮助。我有一个 mp3,想将它添加到我的应用程序中,以便在后台循环运行。 这可能吗?非常感谢。 最佳答案 您可以使用声音库。处理已经自带
我有几个这样创建的按钮: 在 setup() PImage[] imgs1 = {loadImage("AREA1_1.png"),loadImage("AREA1_2.png"),loadImage
我正在尝试使用 Processing 创建一个多人游戏,但无法弄清楚如何将屏幕分成两个以显示玩家的不同情况? 就像在 c# 中一样,我们有Viewport leftViewport,rightView
我一直在尝试使用 Moore 邻域在处理过程中创建元胞自动机,到目前为止非常成功。我已经设法使基本系统正常工作,现在我希望通过添加不同的功能来使用它。现在,我检查细胞是否存活。如果是,我使用 fill
有没有办法用 JavaScript 代码检查资源使用情况?我可以检查脚本的 RAM 使用情况和 CPU 使用情况吗? 由于做某事有多种方法,我可能会使用不同的方法编写代码,并将其保存为两个不同的文件,
我想弄清楚如何处理这样的列表: [ [[4,6,7], [1,2,4,6]] , [[10,4,2,4], [1]] ] 这是一个整数列表的列表 我希望我的函数将此列表作为输入并返回列表中没有重复的整
有没有办法在不需要时处理 MethodChannel/EventChannel ?我问是因为我想为对象创建多个方法/事件 channel 。 例子: class Call { ... fields
我有一个关于在 Python3 中处理 ConnectionResetError 的问题。这通常发生在我使用 urllib.request.Request 函数时。我想知道如果我们遇到这样的错误是否可
我一直在努力解决这个问题几个小时,但无济于事。代码很简单,一个弹跳球(粒子)。将粒子的速度初始化为 (0, 0) 将使其保持上下弹跳。将粒子的初始化速度更改为 (0, 0.01) 或任何十进制浮点数都
我把自己弄得一团糟。 我想在我的系统中添加 python3.6 所以我决定在我的 Ubuntu 19.10 中卸载现有的。但是现在每次我想安装一些东西我都会得到这样的错误: dpkg: error w
我正在努力解决 Rpart 包中的 NA 功能。我得到了以下数据框(下面的代码) Outcome VarA VarB 1 1 1 0 2 1 1 1
我将 Java 与 JSF 一起使用,这是 Glassfish 3 容器。 在我的 Web 应用程序中,我试图实现一个文件(图像)管理系统。 我有一个 config.properties我从中读取上传
所以我一直在Processing工作几个星期以来,虽然我没有编程经验,但我已经转向更复杂的项目。我正在编写一个进化模拟器,它会产生具有随机属性的生物。 最终,我将添加复制,但现在这些生物只是在屏幕上漂
有人知道 Delphi 2009 对“with”的处理有什么不同吗? 我昨天解决了一个问题,只是将“with”解构为完整引用,如“with Datamodule、Dataset、MainForm”。
我是一名优秀的程序员,十分优秀!