@@ -45,7 +45,7 @@ weight = 3
4545
4646選擇函式或方法名稱時,要考慮這個名字將被閱讀的情境。下列建議可協助避免在呼叫端出現多餘的[ 重複] [ repetition ] :
4747
48- [ repetition ] : decisions.md #repetition
48+ [ repetition ] : / decisions/ #repetition
4949
5050- 下列資訊通常可從函式或方法名稱中省略:
5151
@@ -166,7 +166,7 @@ func (c *Config) WriteBinaryTo(w io.Writer) (int64, error)
166166
167167底下範例多以 stub 為主。如果你的程式碼用的是 fake 或其他種類的測試替身,請相應地調整名稱。
168168
169- [naming]: guide.md #naming
169+ [naming]: / guide/ #naming
170170[test doubles]: https:// abseil.io/resources/swe-book/html/ch13.html#basic_concepts
171171
172172假設你有一個焦點明確、提供生產程式碼的套件,內容類似下列:
@@ -492,7 +492,7 @@ func LongFunction() {
492492```
493493
494494[ short variable declarations ] : https://go.dev/ref/spec#Short_variable_declarations
495- [ import renaming ] : decisions.md #import-renaming
495+ [ import renaming ] : / decisions/ #import-renaming
496496
497497<a id =" util-packages " ></a >
498498
@@ -962,7 +962,7 @@ err2 := fmt.Errorf("couldn't find fortune database: %v: %w", err, ErrInternal)
962962- [Go Tip #48 : Error Sentinel Values ]
963963- [Go Tip #106 : Error Naming Conventions ]
964964
965- [commentary]: decisions. md #commentary
965+ [commentary]: / decisions/ #commentary
966966[Go Tip #48 : Error Sentinel Values ]: https:// google.github.io/styleguide/go/index.html#gotip
967967[Go Tip #106 : Error Naming Conventions ]: https:// google.github.io/styleguide/go/index.html#gotip
968968
@@ -1049,7 +1049,7 @@ log.V(2).Infof("Handling %v", sql.Explain())
10491049標準函式庫會對 API 誤用 panic。例如 [` reflect` ] 在許多看起來像被誤解誤用的存取場景時會 panic。這類似於語言核心 bug 的 panic,例如越界存取 slice。Code review 與測試應該要找到這些 bug,它們不該出現在正式環境中。這些 panic 起到的是不變條件檢查的作用,且不依賴任何函式庫,因為標準函式庫無法存取 Google 程式碼庫使用的[分等級的 ` log` ][levelled ` log` ] 套件。
10501050
10511051[` reflect` ]: https:// pkg.go.dev/reflect
1052- [levelled ` log` ]: decisions. md #logging
1052+ [levelled ` log` ]: / decisions/ #logging
10531053
10541054另一個雖然少見、但 panic 可能有用的情況是:作為某個套件內部實作的細節,且呼叫鏈中一定有對應的 ` recover` 。剖析器與類似的深度遞迴、緊密耦合的內部函式群,可以從這種設計中獲益,因為在這類程式中傳遞錯誤回傳值會增加複雜度卻沒有實質價值。
10551055
@@ -1125,7 +1125,7 @@ func answer(i int) string {
11251125
11261126採用熟悉風格撰寫文件的 Go 程式碼,比起寫得不對或完全沒寫文件的程式碼,更容易閱讀,也較不容易被誤用。可執行的[範例][examples]會出現在 Godoc 與 Code Search 中,是說明如何使用程式碼的絕佳方式。
11271127
1128- [examples]: decisions. md #examples
1128+ [examples]: / decisions/ #examples
11291129
11301130<a id=" documentation-conventions-params" ></a>
11311131
@@ -1172,7 +1172,7 @@ func Sprintf(format string, data ...any) string
11721172- [GoTip #41 : Identify Function Call Parameters ]
11731173- [GoTip #51 : Patterns for Configuration ]
11741174
1175- [commentary]: decisions. md #commentary
1175+ [commentary]: / decisions/ #commentary
11761176[GoTip #41 : Identify Function Call Parameters ]: https:// google.github.io/styleguide/go/index.html#gotip
11771177[GoTip #51 : Patterns for Configuration ]: https:// google.github.io/styleguide/go/index.html#gotip
11781178
@@ -1507,7 +1507,7 @@ Go 提供[文件伺服器](https://pkg.go.dev/golang.org/x/pkgsite/cmd/pkgsite)
15071507
15081508[Godoc]: https:// pkg.go.dev/
15091509[format documentation]: https:// go.dev/doc/comment
1510- [runnable examples]: decisions. md #examples
1510+ [runnable examples]: / decisions/ #examples
15111511
15121512<a id=" signal-boost" ></a>
15131513
@@ -1762,8 +1762,8 @@ func sum(values chan int) (out int) {
17621762
17631763[option struct ]: #option-structure
17641764[variadic options]: #variadic-options
1765- [clarity]: guide. md #clarity
1766- [least mechanism]: guide. md #least-mechanism
1765+ [clarity]: / guide/ #clarity
1766+ [least mechanism]: / guide/ #least-mechanism
17671767
17681768<a id=" option-structure" ></a>
17691769
@@ -2019,10 +2019,10 @@ Go 對「測試 helper」與「assertion helper」加以區分:
20192019
20202020測試的目的是回報受測程式碼的成敗狀態。讓測試失敗的理想位置是在 ` Test` 函式本身內,因為這能確保[失敗訊息][failure messages]與測試邏輯都很清楚。
20212021
2022- [mark them as a test helper]: decisions. md #mark-test-helpers
2022+ [mark them as a test helper]: / decisions/ #mark-test-helpers
20232023[error handling in test helpers]: #test-helper-error -handling
2024- [not considered idiomatic]: decisions. md #assert
2025- [failure messages]: decisions. md #useful-test-failures
2024+ [not considered idiomatic]: / decisions/ #assert
2025+ [failure messages]: / decisions/ #useful-test-failures
20262026
20272027當測試程式碼成長時,有時必須把部分功能抽出獨立函式。一般軟體工程的考量仍然適用,因為*測試程式碼也是程式碼*。如果這些功能不與測試框架互動,所有平常的規則都適用。但當共用程式碼會與框架互動時,就要小心避免常見陷阱,以免最後得到沒有資訊量的失敗訊息與難以維護的測試。
20282028
@@ -2082,10 +2082,10 @@ func FuzzFencepost(f *testing.F) {
20822082
20832083**注意:** 測試 helper 與一般函式庫程式碼有相似之處。函式庫中的程式碼通常[不應 panic][not panic],除非在罕見情況;從測試呼叫的程式碼除非[沒必要繼續][no point in proceeding],否則不應終止測試。
20842084
2085- [table-driven test]: decisions. md #table-driven-tests
2086- [useful test failures]: decisions. md #useful-test-failures
2085+ [table-driven test]: / decisions/ #table-driven-tests
2086+ [useful test failures]: / decisions/ #useful-test-failures
20872087[package ` cmp` ]: https:// pkg.go.dev/github.com/google/go-cmp/cmp
2088- [not panic]: decisions. md #dont-panic
2088+ [not panic]: / decisions/ #dont-panic
20892089[no point in proceeding]: #t-fatal
20902090
20912091<a id=" test-validation-apis" ></a>
@@ -2258,7 +2258,7 @@ func TestAcceptance(t *testing.T) {
22582258
22592259**注意:** 本節討論的[測試 helper][test helpers] 是 Go 用語意義下的:做測試 setup 與 cleanup 的函式,而不是常見的 assertion 工具。更多討論請參考[測試函式](#test-functions)一節。
22602260
2261- [test helpers]: decisions. md #mark-test-helpers
2261+ [test helpers]: / decisions/ #mark-test-helpers
22622262
22632263測試 helper 執行的操作有時會失敗。例如:設定一個含檔案的目錄需要 I /O,可能失敗。當測試 helper 失敗時,通常代表測試無法繼續,因為某個 setup 前提條件失敗了。發生這種情況時,在 helper 內呼叫 ` Fatal` 系列函式之一是較好的做法:
22642264
@@ -2878,7 +2878,7 @@ Go 測試預設依序執行,因此上面的測試會以下列順序跑:
28782878
28792879[ 全域狀態有多種形式] ( #globals-forms ) ,也有一些[ 判定何時安全的試紙測試] ( #globals-litmus-tests ) 可用。
28802880
2881- [ Unit Testing Practices on Public APIs ] : index.md #unit-testing-practices
2881+ [ Unit Testing Practices on Public APIs ] : / #unit-testing-practices
28822882
28832883<a id =" globals-forms " ></a >
28842884
0 commit comments