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

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

このコミットは、Go言語の公式ドキュメント doc/go1.2.html におけるスライス(slice)の3インデックス表記に関する説明の誤りを修正するものです。具体的には、スライスの容量(capacity)計算例における数値の誤りを訂正し、より明確な説明となるように調整されています。

コミット

commit 4977f9f926b743a575a2d68920a41806c95d6a76
Author: Rob Pike <r@golang.org>
Date:   Mon Sep 23 14:41:20 2013 +1000

    doc/go1.2.html: fix 3-index slice example
    A number was wrong; adjust as suggested to make things clearer.
    Fixes #6452
    
    R=golang-dev, minux.ma
    CC=golang-dev
    https://golang.org/cl/13422046

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

https://github.com/golang/go/commit/4977f9f926b743a575a2d68920a41806c95d6a76

元コミット内容

doc/go1.2.html: fix 3-index slice example A number was wrong; adjust as suggested to make things clearer. Fixes #6452

変更の背景

このコミットは、Go言語のバージョン1.2のリリースノートおよびドキュメントの一部である doc/go1.2.html に記載されていた、スライスの3インデックス表記に関する説明の誤りを修正するために行われました。具体的には、array[low:high:max] という形式でスライスを作成する際の容量計算の例において、誤った数値が使用されており、その結果として説明が不明瞭になっていました。

この問題は、GoのIssueトラッカーで #6452 として報告されていました。ユーザーからの指摘により、ドキュメントの正確性を保ち、読者の理解を深めるために修正が必要と判断されました。Rob Pike氏によってこの修正がコミットされ、ドキュメントの記述がより正確で分かりやすいものになりました。

前提知識の解説

Go言語のスライス

Go言語におけるスライスは、配列(array)の上に構築された、より強力で柔軟なデータ構造です。スライスは、基となる配列の一部を参照し、動的な長さを持ちます。スライスは以下の3つの要素で構成されます。

  1. ポインタ (Pointer): スライスが参照する基となる配列の先頭要素へのポインタ。
  2. 長さ (Length): スライスに含まれる要素の数。len() 関数で取得できます。
  3. 容量 (Capacity): スライスの基となる配列の、スライスが参照する開始位置から末尾までの要素の数。cap() 関数で取得できます。スライスを拡張できる最大値を示します。

スライスの作成と3インデックス表記

スライスは、既存の配列や他のスライスから作成できます。基本的なスライス作成は array[low:high] の形式で行われ、low から high-1 までの要素を含むスライスが作成されます。この場合、スライスの長さは high - low、容量は基となる配列の末尾までの要素数となります。

Go 1.2から導入された3インデックス表記 array[low:high:max] は、スライスの容量を明示的に指定するために使用されます。

  • low: スライスの開始インデックス。
  • high: スライスの終了インデックス(high-1 までが含まれる)。
  • max: スライスの容量の最大インデックス。スライスの容量は max - low となります。

この表記を使用することで、作成されるスライスの長さと容量をより細かく制御できます。特に、基となる配列の特定の部分のみをスライスとして公開し、それ以上の要素へのアクセスを制限したい場合に有用です。

Go言語のドキュメント

Go言語の公式ドキュメントは、Goの学習と利用において非常に重要なリソースです。go1.2.html のようなファイルは、特定のGoバージョンの新機能や変更点について説明するリリースノートの役割を果たします。これらのドキュメントは、Go言語の挙動を正確に記述している必要があり、誤りがあればユーザーの混乱を招く可能性があります。

技術的詳細

このコミットの技術的な詳細は、Go言語のスライスにおける3インデックス表記の容量計算の正確性にあります。

元のドキュメントでは、以下の例が示されていました。

<pre>
slice = array[2:4:6]
</pre>

<p>
sets the slice to have the same length as in the earlier example but its capacity is now only 4 elements (6-2).
It is impossible to use this new slice value to access the last two elements of the original array.
</p>

ここで、array[2:4:6] の場合、

  • low2
  • high4
  • max6

スライスの長さは high - low = 4 - 2 = 2 となります。 スライスの容量は max - low = 6 - 2 = 4 となります。

この容量の計算 (6-2) は正しいのですが、その後の説明で「It is impossible to use this new slice value to access the last two elements of the original array.」とあり、この「last two elements」という記述が、例示されている array[2:4:6] の文脈では誤解を招く可能性がありました。

修正後のドキュメントでは、例の max の値が 6 から 7 に変更されました。

<pre>
slice = array[2:4:7]
</pre>

<p>
sets the slice to have the same length as in the earlier example but its capacity is now only 5 elements (7-2).
It is impossible to use this new slice value to access the last three elements of the original array.
</p>

この変更により、

  • low2
  • high4
  • max7

スライスの長さは high - low = 4 - 2 = 2 となります(これは変更なし)。 スライスの容量は max - low = 7 - 2 = 5 となります。

そして、説明文も「capacity is now only 5 elements (7-2)」と「It is impossible to use this new slice value to access the last three elements of the original array.」に修正されました。

この修正は、単なる数値の変更だけでなく、その数値が示す意味(スライスの容量と、それによってアクセスできなくなる基となる配列の要素数)をより正確に反映させるためのものです。これにより、読者がスライスの3インデックス表記の挙動、特に容量の制限について正しく理解できるようになりました。

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

変更は doc/go1.2.html ファイル内で行われています。

--- a/doc/go1.2.html
+++ b/doc/go1.2.html
@@ -99,12 +99,12 @@ source slice or array, adjusted for the origin. For instance,
 </p>
 
 <pre>
-slice = array[2:4:6]
+slice = array[2:4:7]
 </pre>
 
 <p>
-sets the slice to have the same length as in the earlier example but its capacity is now only 4 elements (6-2).\n-It is impossible to use this new slice value to access the last two elements of the original array.
+sets the slice to have the same length as in the earlier example but its capacity is now only 5 elements (7-2).\n+It is impossible to use this new slice value to access the last three elements of the original array.
 </p>
 
 <p>

コアとなるコードの解説

この変更は、HTMLドキュメント内のテキストと数値の修正です。

  1. slice = array[2:4:6] から slice = array[2:4:7] への変更: スライスの容量を説明するための例として使用されていた max インデックスの値が 6 から 7 に変更されました。これにより、容量の計算結果が変わります。

  2. 容量の説明の変更:

    • capacity is now only 4 elements (6-2) から capacity is now only 5 elements (7-2) へ変更。これは、max の値が 7 になったことに伴う容量の再計算結果を反映しています。
    • It is impossible to use this new slice value to access the last two elements of the original array. から It is impossible to use this new slice value to access the last three elements of the original array. へ変更。これは、新しい容量 5 に基づいて、基となる配列のどの部分がスライスからアクセス不可能になるかを正確に記述するための修正です。

これらの変更は、Go言語のスライスにおける3インデックス表記の挙動、特に容量の計算とそれによってアクセス範囲が制限されることを、読者がより正確に理解できるようにするためのものです。ドキュメントの正確性を高め、誤解を避けることが目的です。

関連リンク

参考にした情報源リンク

  • Go Issue 6452 の内容
  • Go言語の公式ドキュメント(特にスライスに関するセクション)
  • Go言語のスライスに関する一般的な解説記事
  • コミットメッセージと差分情報