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

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

このコミットは、Go言語の公式ドキュメントである effective_go.html における記述の修正です。具体的には、「byte array」という表現を「byte slice」に置き換えることで、Go言語におけるバイト列の扱いに関する記述をより正確に、かつ現代のGoの慣習に即したものにしています。

コミット

commit c8c8ab08ed4946e719d0ae001d9af3287e73b70d
Author: Nigel Tao <nigeltao@golang.org>
Date:   Sat Jan 19 13:36:59 2013 +1100

    doc: fix effective_go: s/byte array/byte slice/.
    
    R=rsc
    CC=golang-dev, mdempsky
    https://golang.org/cl/7062049

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

https://github.com/golang/go/commit/c8c8ab08ed4946e719d0ae001d9af3287e73b70d

元コミット内容

doc: fix effective_go: s/byte array/byte slice/.

R=rsc
CC=golang-dev, mdempsky
https://golang.org/cl/7062049

変更の背景

この変更の背景には、Go言語における「配列 (array)」と「スライス (slice)」という2つのデータ構造の根本的な違いと、それらのGoプログラミングにおける一般的な使用方法があります。

初期のGo言語のドキュメントや議論では、バイトのシーケンスを指す際に「byte array」という言葉が使われることがありました。しかし、Go言語において、固定長である「配列」と、可変長でより柔軟な「スライス」は明確に区別されます。特に、バイトのシーケンスを扱う場合、ほとんどのGoプログラムでは固定長の配列ではなく、動的にサイズ変更が可能なスライス([]byte)が使用されます。

effective_go.html は、Go言語の「Effective Go」というドキュメントであり、Go言語を効果的に書くための慣習やヒントを提供する非常に重要なガイドラインです。このドキュメントがGoプログラミングのベストプラクティスを伝える上で、誤解を招く可能性のある表現を修正し、より正確な用語を使用することは極めて重要でした。

このコミットは、Go言語の設計思想と実際の使用パターンにドキュメントを合致させるための、用語の厳密化を目的としています。

前提知識の解説

このコミットを理解するためには、Go言語における以下の概念を理解しておく必要があります。

1. 配列 (Array)

  • 定義: Goの配列は、同じ型の要素を連続して格納する固定長のシーケンスです。配列の長さは型の一部であり、コンパイル時に決定されます。
  • 宣言例: var a [10]int (10個の整数を格納する配列)
  • 特徴:
    • 長さが固定されているため、一度宣言するとその長さを変更することはできません。
    • 値型であり、配列を別の変数に代入したり、関数に渡したりすると、配列全体のコピーが作成されます。
    • Goでは、配列を直接使用する機会は比較的少ないです。

2. スライス (Slice)

  • 定義: Goのスライスは、配列の一部を参照する動的なビューです。スライスは、基となる配列へのポインタ、スライスの長さ (length)、および容量 (capacity) の3つの要素で構成されます。
  • 宣言例: var s []int (整数のスライス)、make([]byte, 10, 20) (長さ10、容量20のバイトスライス)
  • 特徴:
    • 長さが動的であり、append 関数などを使用して要素を追加することで、基となる配列の容量が許す限り、または新しい基となる配列が割り当てられることで、長さを変更できます。
    • 参照型であり、スライスを別の変数に代入したり、関数に渡したりしても、基となる配列のコピーは作成されず、同じ基となる配列を参照します。これにより、効率的なデータ共有が可能です。
    • Goプログラミングにおいて、可変長のシーケンスを扱う際の主要なデータ構造です。特に、バイト列 ([]byte) の操作で頻繁に利用されます。

3. effective_go.html (Effective Go)

  • Go言語の公式ドキュメントの一部であり、Go言語の設計原則、慣習、および効果的なプログラミングスタイルについて解説しています。Goプログラマーがより良いコードを書くための重要なリソースです。

技術的詳細

このコミットは、doc/effective_go.html ファイル内の特定の文字列を置換することで、Go言語のドキュメントの正確性を向上させています。

具体的には、以下の置換が行われています。

  • s/byte array/byte slice/
  • s/byte arrays/byte slices/
  • s/input array <a>/input slice <b>/ (これはコード例の変数名も修正しています)
  • s/indexing an array out of bounds/indexing a slice out of bounds/
  • s/an array index out of bounds/an index out of bounds/

これらの変更は、Go言語のバイト列の扱いに関する記述を、より一般的な「スライス」の概念に統一することを目的としています。Goにおいて、バイトのシーケンスは通常 []byte 型のスライスとして表現され、操作されます。固定長のバイト配列 [N]byte が直接使われることは稀であり、ほとんどのI/O操作やデータ処理関数はスライスを受け入れます。

例えば、io.Readerio.Writer インターフェースのメソッドは []byte を引数に取りますし、bytes パッケージの多くの関数もスライスを操作します。したがって、ドキュメント内で「byte array」という表現が使われていると、読者が固定長の配列を想定してしまい、Goの慣習から外れた理解をしてしまう可能性があります。

この修正は、Go言語の設計思想、特にスライスが配列の上に構築された強力で柔軟な抽象化であるという点を、ドキュメントを通じて明確に伝えるためのものです。

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

変更は doc/effective_go.html ファイルのみです。

--- a/doc/effective_go.html
+++ b/doc/effective_go.html
@@ -726,11 +726,11 @@ func shouldEscape(c byte) bool {
 </pre>
  
 <p>
-Here\'s a comparison routine for byte arrays that uses two
+Here\'s a comparison routine for byte slices that uses two
 <code>switch</code> statements:
 </p>
 <pre>
-// Compare returns an integer comparing the two byte arrays,
+// Compare returns an integer comparing the two byte slices,
 // lexicographically.
 // The result will be 0 if a == b, -1 if a &lt; b, and +1 if a &gt; b
 func Compare(a, b []byte) int {
@@ -810,7 +810,7 @@ This is a common style; see the section on error handling for more examples.
 A similar approach obviates the need to pass a pointer to a return
 value to simulate a reference parameter.
 Here\'s a simple-minded function to
-grab a number from a position in a byte array, returning the number
+grab a number from a position in a byte slice, returning the number
 and the next position.
 </p>
  
@@ -827,12 +827,12 @@ func nextInt(b []byte, i int) (int, int) {
 </pre>
  
 <p>
-You could use it to scan the numbers in an input array <code>a</code> like this:\n+You could use it to scan the numbers in an input slice <code>b</code> like this:\n </p>
  
 <pre>
-    for i := 0; i &lt; len(a); {\n-        x, i = nextInt(a, i)\n+    for i := 0; i &lt; len(b); {\n+        x, i = nextInt(b, i)\n         fmt.Println(x)\n     }\n </pre>
@@ -1374,8 +1374,8 @@ var timeZone = map[string] int {
 </pre>
 <p>
 Assigning and fetching map values looks syntactically just like
-doing the same for arrays except that the index doesn\'t need to
-be an integer.\n+doing the same for arrays and slices except that the index doesn\'t\n+need to be an integer.\n </p>
 <pre>
 offset := timeZone[\"EST\"]\n@@ -1500,7 +1500,7 @@ prints
 If you just want the default conversion, such as decimal for integers, you can use
 the catchall format <code>%v</code> (for &ldquo;value&rdquo;); the result is exactly
 what <code>Print</code> and <code>Println</code> would produce.\n-Moreover, that format can print <em>any</em> value, even arrays, structs, and\n+Moreover, that format can print <em>any</em> value, even arrays, slices, structs, and\n maps.  Here is a print statement for the time zone map defined in the previous section.\n </p>
 <pre>
@@ -1544,8 +1544,8 @@ map[string] int{\"CST\":-21600, \"PST\":-28800, \"EST\":-18000, \"UTC\":0, \"MST\":-25200}\n That quoted string format is also available through <code>%q</code> when\n applied to a value of type <code>string</code> or <code>[]byte</code>;\n the alternate format <code>%#q</code> will use backquotes instead if possible.\n-Also, <code>%x</code> works on strings and arrays of bytes as well as on integers,\n-generating a long hexadecimal string, and with\n+Also, <code>%x</code> works on strings, byte arrays and byte slices as well as\n+on integers, generating a long hexadecimal string, and with\n a space in the format (<code>%&nbsp;x</code>) it puts spaces between the bytes.\n </p>
 <p>
@@ -2836,7 +2836,7 @@ func init() {
  
 <p>
 When <code>panic</code> is called, including implicitly for run-time
-errors such as indexing an array out of bounds or failing a type\n+errors such as indexing a slice out of bounds or failing a type\n assertion, it immediately stops execution of the current function\n and begins unwinding the stack of the goroutine, running any deferred\n functions along the way.  If that unwinding reaches the top of the\n@@ -2937,7 +2937,7 @@ that it has the local type <code>Error</code>.\n If it does not, the type assertion will fail, causing a run-time error\n that continues the stack unwinding as though nothing had interrupted\n it.  This check means that if something unexpected happens, such\n-as an array index out of bounds, the code will fail even though we\n+as an index out of bounds, the code will fail even though we\n are using <code>panic</code> and <code>recover</code> to handle\n user-triggered errors.\n </p>\n```

## コアとなるコードの解説

このコミットの「コアとなるコードの変更箇所」は、Go言語のドキュメント `effective_go.html` 内のテキストコンテンツです。Goのソースコード自体が変更されているわけではありません。

変更の目的は、Go言語のバイト列の扱いに関する記述を、より正確で慣用的なものにすることです。

*   **`s/byte array/byte slice/`**:
    *   Go言語では、固定長の「配列 (array)」と、動的な「スライス (slice)」は明確に区別されます。バイトのシーケンスを扱う場合、ほとんどのGoプログラムでは `[]byte` 型のスライスが使用されます。この変更は、ドキュメントがこのGoの慣習を正確に反映するようにします。
*   **`s/input array <a>/input slice <b>/`**:
    *   これは、コード例における変数名の修正です。元のコード例では `a` という変数が「input array」として使われていましたが、これが実際にはスライスとして扱われるべき文脈であったため、変数名を `b` に変更し、「input slice」と明記することで、コード例と説明の一貫性を保っています。
*   **`s/indexing an array out of bounds/indexing a slice out of bounds/` および `s/an array index out of bounds/an index out of bounds/`**:
    *   `panic` の説明箇所で、配列のインデックス範囲外アクセスに関する記述をスライスに適用されるように修正しています。スライスも基となる配列の範囲内でインデックスアクセスを行いますが、Goのランタイムエラーとしての「out of bounds」は、配列だけでなくスライスに対しても発生します。この修正により、より一般的なケースをカバーし、読者の誤解を防ぎます。

これらの変更は、Go言語のドキュメントの品質と正確性を向上させ、Goプログラマーが言語の特性をより深く理解するのに役立ちます。

## 関連リンク

*   Go言語の公式ドキュメント: [https://go.dev/doc/](https://go.dev/doc/)
*   Effective Go: [https://go.dev/doc/effective_go](https://go.dev/doc/effective_go)
*   Go Slices: usage and internals: [https://go.dev/blog/slices](https://go.dev/blog/slices) (Goのスライスに関する詳細なブログ記事)

## 参考にした情報源リンク

*   Go言語の公式ドキュメント (特に Effective Go および Slices のブログ記事)
*   Go言語における配列とスライスの違いに関する一般的なプログラミングリソース