Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[インデックス 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 testgo help buildなどの関連ドキュメントに明示的に追加することで、Goツールの挙動をより透過的にし、ユーザーの理解を深めることにあります。これにより、開発者はファイル命名規則をより効果的に利用して、ビルドやテストの対象から特定のファイルを除外できるようになります。

前提知識の解説

Goコマンド (go tool)

Go言語には、ソースコードのビルド、テスト、依存関係の管理など、開発プロセス全体をサポートする強力なコマンドラインツール「go tool」が付属しています。go buildgo rungo testgo 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.gosrc/cmd/go/help.gosrc/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ツールのドキュメント文字列を直接変更しています。

  1. src/cmd/go/doc.go:

    • go testコマンドの動作説明部分に、*_test.goパターンに一致するファイルが再コンパイルされることに加えて、「ファイル名が__test.goを含む)または.で始まるファイルは無視される」という記述が追加されました。
    • さらに、一般的なGoツールのファイル処理に関するセクションにも、「ファイル名が.または_で始まるファイルはGoツールによって無視される」という独立した文が追加されました。これは、go testだけでなく、go buildなどの他のコマンドにも適用される一般的なルールであることを示唆しています。
  2. src/cmd/go/help.go:

    • go helpコマンドの出力に含まれる一般的なファイル処理のルールに関するセクションに、「ファイル名が.または_で始まるファイルはGoツールによって無視される」という文が追加されました。これにより、ユーザーがgo helpを実行した際に、この重要な情報が提供されるようになります。
  3. src/cmd/go/test.go:

    • go testコマンドのヘルプメッセージを生成する部分に、src/cmd/go/doc.goと同様の変更が加えられ、「ファイル名が__test.goを含む)または.で始まるファイルは無視される」という記述が追加されました。これにより、go help testを実行した際にもこの情報が表示されます。

これらの変更は、Goツールの内部的な挙動を変更するものではなく、その挙動をユーザーに明確に伝えるためのドキュメントの改善です。これにより、Go開発者は、特定のファイルをビルドやテストの対象から除外したい場合に、ファイル名の先頭に_.を付けるという慣習を安心して利用できるようになります。これは、一時的なファイル、未完成のコード、または特定のビルドから除外したいリソースなどを管理する際に特に有用です。

関連リンク

参考にした情報源リンク