Skip to content

Commit eec60c4

Browse files
committed
fix: resolve broken internal links under /go-tutorial/ subpath
- Enable canonifyURLs so Hugo prefixes absolute paths with baseURL - Rewrite 30 reference-style link definitions from .md to site paths
1 parent a783472 commit eec60c4

4 files changed

Lines changed: 31 additions & 30 deletions

File tree

content/best-practices.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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]會出現在 GodocCode 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

content/decisions.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Go 套件名稱不應有底線。如果你必須匯入名稱含底線的套件 (
8787
[`tabwriter`]: https://pkg.go.dev/text/tabwriter
8888
[`k8s`]: https://pkg.go.dev/k8s.io/client-go/kubernetes
8989
[`oauth2`]: https://pkg.go.dev/golang.org/x/oauth2
90-
[shadowed]: best-practices.md#shadowing
90+
[shadowed]: /best-practices/#shadowing
9191

9292
避免使用 `util``utility``common``helper``model``testhelper` 之類沒有資訊量的套件名稱,這會誘使套件使用者[在匯入時改名](#import-renaming)。請參考:
9393

@@ -140,7 +140,7 @@ const (
140140
)
141141
```
142142

143-
[MixedCaps]: guide.md#mixed-caps
143+
[MixedCaps]: /guide/#mixed-caps
144144
[Exported]: https://tour.golang.org/basics/3
145145

146146
不要使用非 MixedCaps 的常數命名,也不要使用 `K` 前綴。
@@ -226,8 +226,8 @@ const (
226226
- 大 scope 通常執行一個或數個大操作,大約 15-25 行。
227227
- 非常大的 scope 是超過一頁的範圍 (例如超過 25 行)。
228228

229-
[clarity]: guide.md#clarity
230-
[concision]: guide.md#concision
229+
[clarity]: /guide/#clarity
230+
[concision]: /guide/#concision
231231

232232
在小 scope 中可能完全清楚的名稱 (例如 `c` 表示 counter),在更大的 scope 中可能就不夠用,需要進一步澄清以便讀者沿著程式碼往下讀時想起它的用途。在含有許多變數、或變數代表類似值或概念的 scope 中,可能需要比 scope 大小所暗示的更長的變數名稱。
233233

@@ -409,16 +409,16 @@ func (db *DB) UserCount() (int, error) {
409409

410410
**提示:** Godoc 使用的特殊格式很少;清單與程式碼片段通常應縮排以避免折行。除了縮排外,通常應避免裝飾。
411411

412-
[doc preview]: best-practices.md#documentation-preview
413-
[documentation conventions]: best-practices.md#documentation-conventions
412+
[doc preview]: /best-practices/#documentation-preview
413+
[documentation conventions]: /best-practices/#documentation-conventions
414414

415415
<a id="comment-line-length"></a>
416416

417417
### 註解行長度
418418

419419
Go 中的註解沒有固定的[行長度][line length]
420420

421-
[line length]: guide.md#line-length
421+
[line length]: /guide/#line-length
422422

423423
長註解行應折行,以確保在不會自動折行的工具中也能閱讀原始碼。如果不確定要折在哪,80 或 100 欄是常見選擇。但這不是硬性截斷;在某些情況下,把長文字折開反而有害。對折行的特定欄寬不作要求。請力求在同一份檔案內保持[一致](/guide/#consistency)
424424

@@ -1845,7 +1845,7 @@ func (w *Worker) Run() {
18451845
[rethinking-slides]: https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view
18461846
[rethinking-video]: https://www.youtube.com/watch?v=5zXAHh5tJqQ
18471847
[When Go programs end]: https://changelog.com/gotime/165
1848-
[Documentation Conventions: Contexts]: best-practices.md#documentation-conventions-contexts
1848+
[Documentation Conventions: Contexts]: /best-practices/#documentation-conventions-contexts
18491849
18501850
<a id="interfaces"></a>
18511851
@@ -2177,8 +2177,8 @@ Flag 只能定義在 `package main` 或同等位置。
21772177
- [Go Tip #80: Dependency Injection Principles](https://google.github.io/styleguide/go/index.html#gotip)
21782178
21792179
[standard `flag` package]: https://golang.org/pkg/flag/
2180-
[mixed caps]: guide.md#mixed-caps
2181-
[complex CLIs]: best-practices.md#complex-clis
2180+
[mixed caps]: /guide/#mixed-caps
2181+
[complex CLIs]: /best-practices/#complex-clis
21822182
[totw-45]: https://abseil.io/tips/45
21832183
21842184
<a id="logging"></a>
@@ -3084,7 +3084,7 @@ Go 標準函式庫提供 [`testing` 套件][`testing` package]。在 Google 程
30843084
[`testing` package]: https://pkg.go.dev/testing
30853085
[composite literal]: https://go.dev/ref/spec#Composite_literals
30863086
[if-with-initializer]: https://go.dev/ref/spec#If_statements
3087-
[清晰、可讀且可維護的測試]: guide.md#clarity
3087+
[清晰、可讀且可維護的測試]: /guide/#clarity
30883088
30893089
<a id="non-decisions"></a>
30903090

content/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ if err := doSomething(); err == nil { // if NO error
167167

168168
[表格驅動測試 (Table-driven testing)]: https://go.dev/wiki/TableDrivenTests
169169
[錯誤處理]: https://go.dev/blog/errors-are-values
170-
[boost]: best-practices.md#signal-boost
170+
[boost]: /best-practices/#signal-boost
171171

172172
<a id="maintainability"></a>
173173

hugo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title = 'Go 風格指南(繁體中文)'
44
theme = 'book'
55

66
enableGitInfo = true
7+
canonifyURLs = true
78

89
[params]
910
BookTheme = 'auto'

0 commit comments

Comments
 (0)