[インデックス 16602] ファイルの概要
このコミットは、Goコマンドのドキュメント、特にgo test
コマンドと一般的なGoツールのファイル処理に関する説明を更新するものです。具体的には、ファイル名が.
または_
で始まるファイルがGoツールによって無視されるという挙動を明示的にドキュメントに追加しています。
コミット
commit 2f70ac19d29480c2e22bd0a9eca18a215c32db6f
Author: Andrew Gerrand <adg@golang.org>
Date: Thu Jun 20 10:29:38 2013 +1000
cmd/go: document that files beginning with . or _ are ignored
Fixes #5655.
R=golang-dev, minux.ma, r
CC=golang-dev
https://golang.org/cl/10410045
GitHub上でのコミットページへのリンク
https://github.com/golang/go/commit/2f70ac19d29480c2e22bd0a9eca18a215c32db6f
元コミット内容
cmd/go: document that files beginning with . or _ are ignored
(cmd/go: .または_で始まるファイルが無視されることをドキュメント化)
Fixes #5655.
(Issue #5655を修正)
変更の背景
このコミットは、Goツールのファイル処理に関する既存の挙動、特にファイル名が.
(ドット)または_
(アンダースコア)で始まるファイルを無視するという仕様が、公式ドキュメントに明記されていなかったことに対するものです。Goのビルドシステムやテストシステムは、特定の命名規則に従わないファイルを自動的に無視する内部ロジックを持っていました。しかし、この挙動がユーザーにとって不明瞭であったため、混乱を招く可能性がありました。
具体的には、関連するIssue #5655("cmd/go: document that files beginning with . or _ are ignored")で議論されたように、ユーザーがテストファイルや通常のGoソースコードを意図的に無視させたい場合に、ファイル名の先頭に_
や.
を付けるという慣習が一部で存在していました。しかし、この挙動が公式に文書化されていなかったため、Goの初心者や、この挙動を知らない開発者にとっては予期せぬ動作として認識されることがありました。
このコミットの目的は、この暗黙のルールをgo help test
やgo help build
などの関連ドキュメントに明示的に追加することで、Goツールの挙動をより透過的にし、ユーザーの理解を深めることにあります。これにより、開発者はファイル命名規則をより効果的に利用して、ビルドやテストの対象から特定のファイルを除外できるようになります。
前提知識の解説
Goコマンド (go
tool)
Go言語には、ソースコードのビルド、テスト、依存関係の管理など、開発プロセス全体をサポートする強力なコマンドラインツール「go
tool」が付属しています。go build
、go run
、go test
、go get
など、様々なサブコマンドがあります。
go test
コマンド
go test
コマンドは、Goパッケージ内のテストを実行するために使用されます。Goのテストは、ファイル名が_test.go
で終わるファイルに記述され、テスト関数、ベンチマーク関数、および例示関数(examples)を含めることができます。go test
は、これらのテストファイルをコンパイルし、実行可能なテストバイナリを生成して実行します。
Goのパッケージとファイル検出
Goのビルドシステムは、特定のディレクトリ内のGoソースファイル(.go
拡張子を持つファイル)を自動的に検出し、それらをパッケージの一部として扱います。しかし、すべての.go
ファイルが常にビルドやテストの対象となるわけではありません。Goツールには、特定のファイルを除外するための内部ルールやビルド制約(build constraints)が存在します。
ビルド制約 (Build Constraints)
Goでは、ファイルの先頭に// +build
ディレクティブを記述することで、特定の環境や条件でのみファイルをコンパイルするように指定できます。例えば、// +build linux
と書かれたファイルはLinux環境でのみコンパイルされます。これは、プラットフォーム固有のコードや、特定のビルド設定に依存するコードを管理する際に非常に有用です。
ファイルの無視規則
多くのプログラミング言語やビルドシステムでは、特定のファイルやディレクトリをビルドプロセスから除外するための規則が存在します。例えば、バージョン管理システムでは.gitignore
ファイルを使用して無視するファイルを指定します。Goツールも同様に、特定の命名規則を持つファイルを自動的に無視する内部的なメカニズムを持っています。このコミットで文書化されるのは、そのメカニズムの一部です。
技術的詳細
このコミットは、Goツールの内部的なファイル無視ロジックを変更するものではなく、そのロジックに関するドキュメントを更新するものです。具体的には、src/cmd/go/doc.go
、src/cmd/go/help.go
、src/cmd/go/test.go
の3つのファイルが変更されています。これらはそれぞれ、go
コマンドの全体的なドキュメント、go help
コマンドの出力、およびgo test
コマンドのヘルプメッセージに関連するソースファイルです。
変更の核心は、以下の文言が追加されたことです。
Files whose names begin with "_" (including "_test.go") or "." are ignored.
(名前が"_"("_test.go"を含む)または"."で始まるファイルは無視されます。)
この文言は、go test
のヘルプメッセージと、go help build
や一般的なgo
コマンドのファイル処理に関する説明箇所に追加されています。これにより、ユーザーはGoツールがどのようにファイルを処理し、どのファイルをビルドやテストの対象から除外するのかを明確に理解できるようになります。
この変更は、Goツールの挙動の透明性を高め、開発者が意図的にファイルを無視させたい場合に、ファイル名の先頭に_
や.
を付けるという慣習を公式にサポートするものです。例えば、一時的なテストファイルや、まだ開発中のコードスニペットをビルドプロセスから除外したい場合に、この規則を利用できます。
コアとなるコードの変更箇所
src/cmd/go/doc.go
--- a/src/cmd/go/doc.go
+++ b/src/cmd/go/doc.go
@@ -396,9 +396,11 @@ It prints a summary of the test results in the format:
followed by detailed output for each failed package.
- 'Go test' recompiles each package along with any files with names ending in
- "_test.go". These additional files can contain test functions,
- benchmark functions, and example functions. See 'go help testfunc' for more.
+ 'Go test' recompiles each package along with any files with names matching
+ the file pattern "*_test.go".
+ Files whose names begin with "_" (including "_test.go") or "." are ignored.
+ These additional files can contain test functions, benchmark functions, and
+ example functions. See 'go help testfunc' for more.
Each listed package causes the execution of a separate test binary.
Test files that declare a package with the suffix "_test" will be compiled as a
@@ -584,6 +586,8 @@ single directory, the command is applied to a single synthesized
package made up of exactly those files, ignoring any build constraints
in those files and ignoring any other files in the directory.
+ File names that begin with "." or "_" are ignored by the go tool.
+
Remote import path syntax
src/cmd/go/help.go
--- a/src/cmd/go/help.go
+++ b/src/cmd/go/help.go
@@ -53,6 +53,8 @@ As a special case, if the package list is a list of .go files from a
single directory, the command is applied to a single synthesized
package made up of exactly those files, ignoring any build constraints
in those files and ignoring any other files in the directory.
+\n+File names that begin with "." or "_" are ignored by the go tool.\n \t`,
}\n
src/cmd/go/test.go
--- a/src/cmd/go/test.go
+++ b/src/cmd/go/test.go
@@ -45,9 +45,11 @@ It prints a summary of the test results in the format:
followed by detailed output for each failed package.
- 'Go test' recompiles each package along with any files with names ending in
- "_test.go". These additional files can contain test functions,
- benchmark functions, and example functions. See 'go help testfunc' for more.
+ 'Go test' recompiles each package along with any files with names matching
+ the file pattern "*_test.go".
+ Files whose names begin with "_" (including "_test.go") or "." are ignored.
+ These additional files can contain test functions, benchmark functions, and
+ example functions. See 'go help testfunc' for more.
Each listed package causes the execution of a separate test binary.\n
Test files that declare a package with the suffix "_test" will be compiled as a
コアとなるコードの解説
このコミットは、Goツールのドキュメント文字列を直接変更しています。
-
src/cmd/go/doc.go
:go test
コマンドの動作説明部分に、*_test.go
パターンに一致するファイルが再コンパイルされることに加えて、「ファイル名が_
(_test.go
を含む)または.
で始まるファイルは無視される」という記述が追加されました。- さらに、一般的なGoツールのファイル処理に関するセクションにも、「ファイル名が
.
または_
で始まるファイルはGoツールによって無視される」という独立した文が追加されました。これは、go test
だけでなく、go build
などの他のコマンドにも適用される一般的なルールであることを示唆しています。
-
src/cmd/go/help.go
:go help
コマンドの出力に含まれる一般的なファイル処理のルールに関するセクションに、「ファイル名が.
または_
で始まるファイルはGoツールによって無視される」という文が追加されました。これにより、ユーザーがgo help
を実行した際に、この重要な情報が提供されるようになります。
-
src/cmd/go/test.go
:go test
コマンドのヘルプメッセージを生成する部分に、src/cmd/go/doc.go
と同様の変更が加えられ、「ファイル名が_
(_test.go
を含む)または.
で始まるファイルは無視される」という記述が追加されました。これにより、go help test
を実行した際にもこの情報が表示されます。
これらの変更は、Goツールの内部的な挙動を変更するものではなく、その挙動をユーザーに明確に伝えるためのドキュメントの改善です。これにより、Go開発者は、特定のファイルをビルドやテストの対象から除外したい場合に、ファイル名の先頭に_
や.
を付けるという慣習を安心して利用できるようになります。これは、一時的なファイル、未完成のコード、または特定のビルドから除外したいリソースなどを管理する際に特に有用です。
関連リンク
- Go Issue #5655: cmd/go: document that files beginning with . or _ are ignored
- Go CL 10410045: cmd/go: document that files beginning with . or _ are ignored
参考にした情報源リンク
- Go Issue #5655
- Go CL 10410045
- Go Documentation (go command)
- Go Documentation (go test command)
- Go Modules: Excluding files (これはコミット時点では存在しないが、関連する概念として現代のGo開発で参照される可能性のある情報源)
- Go build constraints