mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2026-02-08 19:02:47 +08:00
ch11: fix code path
This commit is contained in:
@@ -11,9 +11,8 @@
|
||||
|
||||
下面的代碼演示了爲用戶提供網絡存儲的web服務中的配額檢測邏輯. 當用戶使用了超過 90% 的存儲配額之後將發送提醒郵件.
|
||||
|
||||
<u><i>gopl.io/ch11/storage1</i></u>
|
||||
```Go
|
||||
gopl.io/ch11/storage1
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
@@ -52,9 +51,8 @@ func CheckQuota(username string) {
|
||||
|
||||
我們想測試這個代碼, 但是我們併不希望發送眞實的郵件. 因此我們將郵件處理邏輯放到一個私有的 notifyUser 函數.
|
||||
|
||||
<u><i>gopl.io/ch11/storage2</i></u>
|
||||
```Go
|
||||
gopl.io/ch11/storage2
|
||||
|
||||
var notifyUser = func(username, msg string) {
|
||||
auth := smtp.PlainAuth("", sender, password, hostname)
|
||||
err := smtp.SendMail(hostname+":587", auth, sender,
|
||||
@@ -130,5 +128,3 @@ func TestCheckQuotaNotifiesUser(t *testing.T) {
|
||||
這種處理模式可以用來暫時保存和恢複所有的全局變量, 包括命令行標誌參數, 調試選項, 和優化參數; 安裝和移除導致生産代碼産生一些調試信息的鉤子函數; 還有有些誘導生産代碼進入某些重要狀態的改變, 比如 超時, 錯誤, 甚至是一些刻意製造的併發行爲.
|
||||
|
||||
以這種方式使用全局變量是安全的, 因爲 go test 併不會同時併發地執行多個測試.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user