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

[インデックス 17745] ファイルの概要

このコミットは、Go言語の仕様書(doc/go_spec.html)に対する変更です。主な目的は、仕様書の記述をより正確かつ包括的にすることであり、Go言語のセマンティクス(意味論)自体に変更はありません。具体的には、宣言が束縛する対象に「ラベル」が追加され、関連する箇所にリンクが追加されています。

コミット

commit 4cc71e336300d50a50b006779207a19deb0c715e
Author: Robert Griesemer <gri@golang.org>
Date:   Thu Oct 3 16:38:22 2013 -0700

    spec: added additional links, added missing 'label'
    
    No semantic spec changes.
    
    R=r
    CC=golang-dev
    https://golang.org/cl/14363043

GitHub上でのコミットページへのリンク

https://github.com/golang/go/commit/4cc71e336300d50a50b006779207a19deb0c715e

元コミット内容

spec: added additional links, added missing 'label'

No semantic spec changes.

R=r
CC=golang-dev
https://golang.org/cl/14363043

変更の背景

このコミットの背景には、Go言語の仕様書(Go Spec)の正確性と網羅性を向上させるという目的があります。Go言語の仕様書は、言語の挙動を定義する最も重要なドキュメントであり、その記述は厳密でなければなりません。

Go言語では、識別子(identifier)が様々な要素に束縛(bind)されます。これには定数、型、変数、関数、パッケージなどが含まれますが、Go言語にはgoto文やbreak/continue文の対象となる「ラベル(label)」という概念も存在します。しかし、以前の仕様書の記述では、宣言が束縛する対象としてラベルが明示的にリストアップされていませんでした。

このコミットは、既存の言語セマンティクスを変更するものではなく、単に仕様書の記述が現実の言語機能と完全に一致するように修正することを目的としています。また、関連する概念への内部リンクを追加することで、読者が仕様書内をよりスムーズに移動し、関連情報を参照できるようにすることも意図されています。

前提知識の解説

このコミットを理解するためには、以下のGo言語の基本的な概念と、仕様書に関する知識が必要です。

  • Go言語の仕様書 (Go Spec): Go言語の文法、セマンティクス、標準ライブラリの挙動などを定義する公式ドキュメントです。Go言語の「真実の源」として機能し、コンパイラやツールの実装、そして開発者の理解の基盤となります。
  • 識別子 (Identifier): プログラム内で名前を付けるために使用される文字列です。変数名、関数名、型名、パッケージ名などが識別子に該当します。
  • 宣言 (Declaration): 識別子を特定のエンティティ(定数、型、変数、関数、パッケージ、ラベルなど)に束縛する行為です。Go言語では、識別子を使用する前に必ず宣言する必要があります。
  • スコープ (Scope): 宣言された識別子が有効であるソースコードの領域を指します。Go言語は字句スコープ(lexical scope)を採用しており、スコープはブロック({}で囲まれた領域)によって決定されます。
  • ブロック (Block): 宣言や文のグループを囲むための構文要素です。関数本体、if文、for文、switch文などの本体がブロックを形成します。
  • ラベル (Label): goto文のジャンプ先や、break/continue文が対象とするループやswitch文を指定するために使用される識別子です。ラベルはコロン(:)で終わる識別子として定義されます。 例:
    func main() {
    Loop:
        for i := 0; i < 10; i++ {
            if i == 5 {
                break Loop // Loopラベルが付いたforループを抜ける
            }
            println(i)
        }
    }
    
  • 空白識別子 (Blank Identifier): アンダーバー(_)で表される特別な識別子です。宣言で使用された場合、新しい束縛を導入せず、値を破棄するために使用されます。例えば、関数の戻り値の一部を無視する場合などに使われます。
  • 字句スコープ (Lexical Scoping): 変数やその他の識別子のスコープが、それらがソースコード内で宣言された場所によって決定される規則です。Go言語は字句スコープを採用しており、内側のブロックは外側のブロックの識別子にアクセスできますが、その逆はできません。
  • Universe Block (ユニバースブロック): Go言語において、すべてのGoプログラムで常に利用可能な組み込みの識別子(int, string, true, false, nil, make, lenなど)が宣言されている最も外側のスコープです。

技術的詳細

このコミットは、doc/go_spec.htmlファイル内の以下のセクションに焦点を当てています。

  1. "Declarations and scope" (宣言とスコープ) の導入部分: 以前の記述では、「宣言は非空白識別子を定数、型、変数、関数、またはパッケージに束縛する」とされていました。このコミットでは、このリストに「ラベル」が追加され、さらに各要素(定数、型、変数、関数、ラベル、パッケージ)への内部リンクが追加されました。これにより、読者は各概念の詳細な説明に直接ジャンプできるようになります。

    変更前:

    A declaration binds a non-<a href="#Blank_identifier">blank</a>
    identifier to a constant, type, variable, function, or package.
    

    変更後:

    A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a
    <a href="#Constant_declarations">constant</a>,
    <a href="#Type_declarations">type</a>,
    <a href="#Variable_declarations">variable</a>,
    <a href="#Function_declarations">function</a>,
    <a href="#Labeled_statements">label</a>, or
    <a href="#Import_declarations">package</a>.
    
  2. スコープの定義部分: 「宣言された識別子のスコープは、識別子が指定された定数、型、変数、関数、またはパッケージを示すソーステキストの範囲である」という記述も同様に修正され、ラベルが追加されました。

    変更前:

    the identifier denotes the specified constant, type, variable, function, or package.
    

    変更後:

    the identifier denotes the specified constant, type, variable, function, label, or package.
    
  3. 字句スコープの解説部分: 「Goはブロックを使用して字句スコープである」という記述に、ブロックへの内部リンクが追加されました。

    変更前:

    Go is lexically scoped using blocks:
    

    変更後:

    Go is lexically scoped using <a href="#Blocks">blocks</a>:
    
  4. ユニバースブロックのスコープに関する記述: 「事前宣言された識別子のスコープはユニバースブロックである」という記述に、事前宣言された識別子への内部リンクが追加されました。

    変更前:

    <li>The scope of a predeclared identifier is the universe block.</li>
    

    変更後:

    <li>The scope of a <a href="#Predeclared_identifiers">predeclared identifier</a> is the universe block.</li>
    
  5. 空白識別子の説明部分: 「空白識別子は、他の識別子と同様に宣言で使用できるが、その宣言は新しい束縛を導入しない」という記述に、「束縛(binding)」への内部リンクが追加されました。

    変更前:

    any other identifier but the declaration does not introduce a new binding.
    

    変更後:

    any other identifier but the declaration does not introduce a new <a href="#Declarations_and_scope">binding</a>.
    

これらの変更は、Go言語の仕様書がより正確で、相互参照が容易になるようにするための細かな改善です。言語の動作自体には影響を与えませんが、仕様書の品質向上に貢献しています。

コアとなるコードの変更箇所

--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -1512,8 +1512,13 @@ Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
 <h2 id="Declarations_and_scope">Declarations and scope</h2>
 
 <p>
-A declaration binds a non-<a href="#Blank_identifier">blank</a>
-identifier to a constant, type, variable, function, or package.
+A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a
+<a href="#Constant_declarations">constant</a>,
+<a href="#Type_declarations">type</a>,
+<a href="#Variable_declarations">variable</a>,
+<a href="#Function_declarations">function</a>,
+<a href="#Labeled_statements">label</a>, or
+<a href="#Import_declarations">package</a>.
 Every identifier in a program must be declared.
 No identifier may be declared twice in the same block, and
 no identifier may be declared in both the file and package block.
@@ -1526,15 +1531,15 @@ TopLevelDecl  = Declaration | FunctionDecl | MethodDecl .
 
 <p>
 The <i>scope</i> of a declared identifier is the extent of source text in which
-the identifier denotes the specified constant, type, variable, function, or package.
+the identifier denotes the specified constant, type, variable, function, label, or package.
 </p>
 
 <p>
-Go is lexically scoped using blocks:
+Go is lexically scoped using <a href="#Blocks">blocks</a>:
 </p>
 
 <ol>
-\t<li>The scope of a predeclared identifier is the universe block.</li>
+\t<li>The scope of a <a href="#Predeclared_identifiers">predeclared identifier</a> is the universe block.</li>
 
 	<li>The scope of an identifier denoting a constant, type, variable,
 	    or function (but not method) declared at top level (outside any
@@ -1589,7 +1594,7 @@ the body of any nested function.
 
 <p>
 The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like
-any other identifier but the declaration does not introduce a new binding.
+any other identifier but the declaration does not introduce a new <a href="#Declarations_and_scope">binding</a>.
 </p>
 
 

コアとなるコードの解説

このコミットは、HTMLドキュメントであるGo言語の仕様書(doc/go_spec.html)のテキストとリンクを修正しています。

  1. 宣言の対象リストの拡張とリンクの追加: A declaration binds a non-<a href="#Blank_identifier">blank</a> identifier to a ... の行が変更されています。

    • 以前は constant, type, variable, function, or package. と列挙されていました。
    • 変更後は、これに加えて <a href="#Labeled_statements">label</a> が追加されています。
    • さらに、各要素(constant, type, variable, function, label, package)に対して、それぞれの詳細な説明セクションへのHTMLアンカーリンク(例: <a href="#Constant_declarations">constant</a>)が追加されています。これにより、読者は宣言の対象についてより深く掘り下げたい場合に、クリック一つで関連セクションに移動できるようになります。
  2. スコープの定義における「ラベル」の追加: the identifier denotes the specified constant, type, variable, function, or package. の行が変更されています。

    • ここでも同様に、識別子が示す対象として label が追加されています。これは、ラベルもまた識別子によって示されるエンティティであり、スコープを持つことを明確にするためです。
  3. 字句スコープの説明におけるリンクの追加: Go is lexically scoped using blocks: の行が変更され、blocks<a href="#Blocks">blocks</a> というリンクが追加されています。これにより、「ブロック」の概念について詳しく知りたい読者が、仕様書内の該当セクションに簡単にアクセスできるようになります。

  4. 事前宣言された識別子のスコープの説明におけるリンクの追加: The scope of a predeclared identifier is the universe block. の行が変更され、predeclared identifier<a href="#Predeclared_identifiers">predeclared identifier</a> というリンクが追加されています。これは、Go言語の組み込み識別子について詳しく知りたい読者への利便性を高めます。

  5. 空白識別子の説明におけるリンクの追加: the declaration does not introduce a new binding. の行が変更され、binding<a href="#Declarations_and_scope">binding</a> というリンクが追加されています。これは、「束縛」という概念が「宣言とスコープ」のセクションで定義されていることを示し、読者がその定義を参照できるようにします。

これらの変更は、HTMLのアンカータグ(<a href="...">)を使用して、仕様書内の異なるセクション間を相互参照できるようにすることで、ドキュメントのナビゲーション性と理解度を向上させています。セマンティックな変更は一切なく、純粋にドキュメントの品質改善を目的としたものです。

関連リンク

参考にした情報源リンク

  • Go言語の仕様書 (Go Spec): https://golang.org/ref/spec
  • Go言語のブログや公式ドキュメント (Go言語の概念理解のため)
  • GitおよびGitHubのドキュメント (コミット情報の解析のため)
  • HTMLのアンカータグに関するMDN Web Docsなど (HTMLの変更内容理解のため)
  • Go言語のラベルに関する情報 (例: https://go.dev/ref/spec#Labeled_statements)