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

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

このコミットは、Go言語の公式ドキュメントの一部であるdoc/docs.htmldoc/go1.2.htmlの更新に関するものです。具体的には、Go 1.2のリリースノートに、fmtパッケージとtext/templateパッケージにおける重要な変更点に関する記述を追加し、さらにdocs.htmlからgo1.2.htmlへのリンクを設置しています。これは、Go 1.2のリリースに向けたドキュメント整備の一環であり、ユーザーが新しいバージョンでの変更内容を把握しやすくすることを目的としています。

コミット

commit a97a7c5eb6e0bca13076c3ce40c8c1f1f020cca6
Author: Rob Pike <r@golang.org>
Date:   Thu Sep 12 09:08:59 2013 +1000

    doc/go1.2.html: some library changes (fmt, template)
    Also link it to the landing page for docs.
    
    R=golang-dev, adg
    CC=golang-dev
    https://golang.org/cl/13652045

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

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

元コミット内容

doc/go1.2.html: some library changes (fmt, template)
Also link it to the landing page for docs.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/13652045

変更の背景

このコミットは、Go 1.2のリリースに先立って、その変更点を公式ドキュメントに反映させるためのものです。Go言語は後方互換性を重視していますが、新しいバージョンではパフォーマンスの改善、新機能の追加、既存ライブラリの機能拡張などが行われます。これらの変更をユーザーに正確に伝えるため、リリースノートは非常に重要な役割を果たします。

特に、fmtパッケージとtext/templateパッケージは、Goアプリケーション開発において頻繁に利用される基本的なライブラリです。これらのパッケージの機能拡張は、開発者のコーディングスタイルやアプリケーションの振る舞いに直接影響を与える可能性があるため、詳細な説明が求められます。

このコミットの目的は、以下の2点に集約されます。

  1. Go 1.2リリースノートの充実: fmtパッケージの書式指定における引数インデックス機能と、text/templateおよびhtml/templateパッケージにおける比較関数の追加およびelse if構文の簡素化について、詳細な説明をgo1.2.htmlに追加すること。
  2. ドキュメントへのアクセス性向上: doc/docs.html(ドキュメントのランディングページ)からgo1.2.htmlへのリンクを追加し、ユーザーがGo 1.2の変更点に容易にアクセスできるようにすること。

前提知識の解説

このコミットの変更内容を理解するためには、以下のGo言語の基本的な概念とパッケージに関する知識が必要です。

  • Go言語のドキュメント構造: Go言語の公式ドキュメントは、golang.org上で提供されており、各バージョンのリリースノートやパッケージのリファレンスなどが含まれます。doc/docs.htmlはドキュメントのトップページのような役割を果たし、他の重要なドキュメントへのリンクを提供します。
  • fmtパッケージ: Go言語における基本的な書式設定I/Oを扱うパッケージです。Printf, Sprintf, Fprintfなどの関数を提供し、様々なデータ型を整形して出力する機能を持っています。C言語のprintf関数に似ていますが、Go独自の機能も含まれます。
  • text/templateパッケージ: テキストベースのテンプレートを扱うためのパッケージです。Go言語のデータ構造をテンプレートにバインドし、動的にテキストを生成する際に使用されます。WebアプリケーションのHTML生成や、設定ファイルの生成など、幅広い用途で利用されます。
  • html/templateパッケージ: text/templateパッケージをベースに、HTMLの自動エスケープ機能を追加したパッケージです。クロスサイトスクリプティング(XSS)などのセキュリティ脆弱性を防ぐために、WebアプリケーションでHTMLを生成する際に推奨されます。
  • sync/atomicパッケージ: 低レベルなアトミック操作を提供するパッケージです。ミューテックスなどのロック機構を使用せずに、共有変数への安全なアクセスを実現するために使用されます。主に並行処理のパフォーマンスが重要な場面で利用されます。
  • Goのリリースサイクルと後方互換性: Go言語は、半年に一度のペースでメジャーリリースを行います。Go 1以降は、Go 1の仕様に準拠するプログラムは将来のGo 1.xリリースでも動作することが保証されています(Go 1 Compatibility Promise)。リリースノートは、この互換性保証の範囲内で導入された新機能や変更点をユーザーに伝える役割を担います。

技術的詳細

このコミットは、Go 1.2のリリースノートであるdoc/go1.2.htmlに、以下の主要なライブラリ変更に関する詳細な説明を追加しています。

1. fmtパッケージの書式指定における引数インデックス機能

Go 1.2では、fmtパッケージのPrintfなどの書式設定関数において、引数を任意の順序で参照できるインデックス機能が導入されました。

  • 機能: 書式指定子(例: %c, %v)の直前に[n]という形式でインデックスを指定することで、引数リストのn番目の引数(1-indexed)を参照できます。
  • モチベーション: 主に国際化(Localization)の文脈で、言語によって単語の順序が異なる場合に、書式文字列を変更するだけで対応できるようにするために導入されました。しかし、それ以外の用途、例えばデバッグログで値とその型を同時に出力する際などにも有用です。
  • 動作: [n]で引数を参照した後、次に通常の処理でフェッチされる引数はn+1番目になります。これにより、インデックス参照と通常の順次参照を組み合わせることが可能です。
  • :
    fmt.Sprintf("%[3]c %[1]c %c\n", 'a', 'b', 'c')
    
    この例では、[3]cが3番目の引数'c'を、[1]cが1番目の引数'a'を、そして最後の%cがその次の引数(この場合は2番目の引数'b')を参照します。結果は "c a b" となります。
  • 後方互換性: この変更は書式指定の構文に対するものであり、既存のプログラムに影響を与えることはありません。

2. text/templateおよびhtml/templateパッケージの機能拡張

Go 1.2では、テンプレートパッケージに以下の2つの変更が加えられました。

a. 新しい比較関数の追加

  • 機能: テンプレート内で基本的な型の比較を行うための新しいデフォルト関数が追加されました。これにはeq (==), ne (!=), lt (<), le (<=), gt (>), ge (>=) が含まれます。
  • Goの演算子との違い:
    • 対象型: これらの関数はbool, int, float64, stringなどの基本的な型にのみ適用されます。Go言語の演算子とは異なり、配列や構造体の比較はできません。
    • 型変換: int8int16のように、異なるが互換性のある整数型同士の比較が可能です。Go言語の演算子では、厳密な型の一致が求められます。
    • eq関数の多引数対応: eq関数は、最初の引数を1つ以上の後続の引数と比較できます。例えば、{{if eq .A 1 2 3}}は、.Aが1、2、または3のいずれかに等しい場合に真となります。
  • 後方互換性: 既存のプログラムには影響しません。もしユーザーが独自の関数マップを通じて同じ名前の関数を定義していた場合、そのユーザー定義関数が新しいデフォルト関数をオーバーライドするため、既存の動作は維持されます。

b. else if構文の簡素化

  • 機能: テンプレートのifチェーンをより簡潔に記述できるよう、else if構文が導入されました。
  • 変更前:
    {{if eq .A 1}} X {{else}} {{if eq .A 2}} Y {{end}} {{end}}
    
  • 変更後:
    {{if eq .A 1}} X {{else if eq .A 2}} Y {{end}}
    
  • 効果: 意味的には同じですが、ネストされたif文を減らし、テンプレートの可読性を向上させます。
  • 後方互換性: 既存のプログラムには影響しません。これは単なる文法の糖衣構文(syntactic sugar)であり、既存のテンプレートは引き続き動作します。

3. sync/atomicパッケージのSwap関数の追加

Go 1.2では、sync/atomicパッケージに新しいSwap関数群が追加されました。

  • 機能: ポインタが指す値と引数の値をアトミックに交換し、交換前の値を返します。
  • 追加された関数: SwapInt32, SwapInt64, SwapUint32, SwapUint64, SwapUintptr, SwapPointer (これはunsafe.Pointerを交換します)。
  • 目的: 並行処理において、ロックを使用せずに変数の値を安全に更新する(交換する)ためのプリミティブを提供します。これにより、特定のシナリオでのパフォーマンス向上が期待できます。

このコミット自体は、これらの機能の実装ではなく、Go 1.2のリリースノートにそれらの変更を記述し、ドキュメント構造に組み込む作業です。

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

このコミットで変更されたファイルは以下の2つです。

  1. doc/docs.html:

    • Go 1.2のリリースノートへのリンク(go1.2.html)が追加されました。
    --- a/doc/docs.html
    +++ b/doc/docs.html
    @@ -88,6 +88,12 @@ A list of significant changes in Go 1.1, with instructions for updating your
     code where necessary.
     </p>
     
    +<h3 id=\"go1.2notes\"><a href=\"/doc/go1.2.html\">Go 1.2 Release Notes</a></h3>
    +<p>
    +A list of significant changes in Go 1.2, with instructions for updating your
    +code where necessary.
    +</p>
    +
     <h3 id=\"go1compat\"><a href=\"/doc/go1compat.html\">Go 1 and the Future of Go Programs</a></h3>
     <p>
     What Go 1 defines and the backwards-compatibility guarantees one can expect as
    
  2. doc/go1.2.html:

    • fmtパッケージの引数インデックス機能に関する詳細な説明が追加されました。
    • text/templateおよびhtml/templateパッケージの比較関数とelse if構文に関する詳細な説明が追加されました。
    • sync/atomicパッケージのSwap関数に関する説明が追加されました。
    • その他、既存のTODOコメントの修正や、リンクの修正などが行われています。
    --- a/doc/go1.2.html
    +++ b/doc/go1.2.html
    @@ -269,83 +269,176 @@ to list them all here, but the following major changes are worth noting:
     </p>
     
     <ul>
    -<li>compress/bzip2: faster decompression by 30% (CL 9915043).\
    +<li>compress/bzip2: TODO faster decompression by 30% (CL 9915043).\
     </li>
     
    -<li>crypto/des: 5x faster encoding/decoding (CL 11874043, 12072045).\
    +<li>crypto/des: TODO 5x faster encoding/decoding (CL 11874043, 12072045).\
     </li>
     
    -<li>encoding/json: faster encoding (CL 9129044).\
    +<li>encoding/json: TODO faster encoding (CL 9129044).\
     </li>
     
    -<li>net: improve windows performance by up to 30% (CL 8670044).\
    +<li>net: TODO improve windows performance by up to 30% (CL 8670044).\
     </li>
     
    -<li>net: improve performance on BSD by up to 30% (CL 8264043, 12927048, 13080043).\
    +<li>net: TODO improve performance on BSD by up to 30% (CL 8264043, 12927048, 13080043).\
     </li>
     </ul>
     
     <h2 id=\"library\">Changes to the standard library</h2>
     
    -<h3 id=\"foo_bar\">foo.Bar</h3>
    +\n+<h3 id=\"archive_tar_zip\">The archive/tar and archive/zip packages</h3>\
     
     <p>\
    -TODO: choose which to call out\
    -<font color=red>\
    -The various routines to scan textual input in the\
    -<a href=\"/pkg/bufio/\"><code>bufio</code></a>\
    -package,\
    -<a href=\"/pkg/bufio/#Reader.ReadBytes\"><code>ReadBytes</code></a>,\
    -<a href=\"/pkg/bufio/#Reader.ReadString\"><code>ReadString</code></a>\
    -and particularly\
    -<a href=\"/pkg/bufio/#Reader.ReadLine\"><code>ReadLine</code></a>,\
    -are needlessly complex to use for simple purposes.\
    -In Go 1.1, a new type,\
    -<a href=\"/pkg/bufio/#Scanner\"><code>Scanner</code></a>,\
    -has been added to make it easier to do simple tasks such as\
    -read the input as a sequence of lines or space-delimited words.\
    -It simplifies the problem by terminating the scan on problematic\
    -input such as pathologically long lines, and having a simple\
    -default: line-oriented input, with each line stripped of its terminator.\
    -Here is code to reproduce the input a line at a time:\
    -</font>\
    +Breaking change: TODO \
    +archive/tar,archive/zip: fix os.FileInfo implementation to provide base name only (CL 13118043).\
    +</p>\
    +\n+<h3 id=\"encoding\">The new encoding package</h3>\
     
     <p>\
    -<font color=red>\
    -<em>Updating</em>:\
    -To correct breakage caused by the new struct field,\
    -<code>go fix</code> will rewrite code to add tags for these types.\
    -More generally, <code>go vet</code> will identify composite literals that\
    -should be revised to use field tags.\
    -</font>\
    +encoding: TODO new package defining generic encoding interfaces (CL 12541051).\
     </p>\
     
    -<ul>\
    +<h3 id=\"fmt_indexed_arguments\">The fmt package</h3>\
     
    -<li>\
    -Breaking change:\
    -archive/tar,archive/zip: fix os.FileInfo implementation to provide base name only (CL 13118043).\
    -</li>\
    +\n+<p>\
    +The <a href=\"/pkg/fmt/\"><code>fmt</code></a> package\'s formatted print\
    +routines such as <a href=\"/pkg/fmt/#Printf\"><code>Printf</code></a>\
    +now allow the data items to be printed to be accessed in arbitrary order\
    +by using an indexing operation in the formatting specifications.\
    +Wherever an argument is to be fetched from the argument list for formatting,\
    +either as the value to be formatted or as a width or specification integer,\
    +a new optional indexing notation <code>[</code><em>n</em><code>]</code>\
    +fetches argument <em>n</em> instead.\
    +The value of <em>n</em> is 1-indexed.\
    +After such an indexing operating, the next argument to be fetched by normal\
    +processing will be <em>n</em>+1.\
    +</p>\
     
    -<li>\
    -encoding: new package defining generic encoding interfaces (CL 12541051).\
    -</li>\
    +\n+<p>\
    +For example, the normal <code>Printf</code> call\
    +</p>\
     
    -<li>\
    -fmt: indexed access to arguments in Printf etc. (CL 9680043).\
    -</li>\
    +\n+<pre>\
    +fmt.Sprintf(\"%c %c %c\\n\", \'a\', \'b\', \'c\')\
    +</pre>\
     
    -<li>\
    -sync/atomic: add Swap functions (CL 12670045).\
    -</li>\
    -<li>\
    -text/template: add comparison functions (CL 13091045).\
    -</li>\
    +\n+<p>\
    +would create the string <code>\"a b c\"</code>, but with indexing operations like this,\
    +</p>\
     
    -<li>\
    -text/template: allow {{\"{{\"}}else if ... {{\"}}\"}} to simplify if chains (CL 13327043).\
    -</li>\
    -</ul>\
    +\n+<pre>\
    +fmt.Sprintf(\"%[3]c %[1]c %c\\n\", \'a\', \'b\', \'c\')\
    +</pre>\
    +\n+<p>\
    +the result is \"<code>\"c a b\"</code>. The <code>[3]</code> index accesses the third formatting\
    +argument, whch is <code>\'c\'</code>, <code>[1]</code> accesses the first, <code>\'a\'</code>,\
    +and then the next fetch accesses the argument following that one, <code>\'b\'</code>.\
    +</p>\
    +\n+<p>\
    +The motivation for this feature is programmable format statements to access\
    +the arguments in different order for localization, but it has other uses:\
    +</p>\n+\n+<pre>\
    +log.Printf(\"trace: value %v of type %[1]T\\n\", expensiveFunction(a.b[c]))\
    +</pre>\n+\n+<p>\
    +<em>Updating</em>: The change to the syntax of format specifications\
    +is strictly backwards compatible, so it affects no working programs.\
    +</p>\n+\n+<h3 id=\"text_template\">The text/template and html/template packages</h3>\
    +\n+<p>\
    +The\n+<a href=\"/pkg/text/template/\"><code>text/template</code></a> package\n+has a couple of changes in Go 1.2, both of which are also mirrored in the\n+<a href=\"/pkg/html/template/\"><code>html/template</code></a> package.\
    +</p>\n+\n+<p>\
    +First, there are new default functions for comparing basic types.\
    +The functions are listed in this table, which shows their names and\n+the associated familiar comparison operator.\
    +</p>\n+\n+<table cellpadding=\"0\" summary=\"Template comparison functions\">\n++<tr>\n++<th width=\"50\"></th><th width=\"100\">Name</th> <th width=\"50\">Operator</th>\n++</tr>\n++<tr>\n++<td></td><td><code>eq</code></td> <td><code>==</code></td>\n++</tr>\n++<tr>\n++<td></td><td><code>ne</code></td> <td><code>!=</code></td>\n++</tr>\n++<tr>\n++<td></td><td><code>lt</code></td> <td><code>&lt;</code></td>\n++</tr>\n++<tr>\n++<td></td><td><code>le</code></td> <td><code>&lt;=</code></td>\n++</tr>\n++<tr>\n++<td></td><td><td><code>gt</code></td> <td><code>&gt;</code></td>\n++</tr>\n++<tr>\n++<td></td><td><code>ge</code></td> <td><code>&gt;=</code></td>\n++</tr>\n++</table>\n+\n+<p>\n+These functions behave slightly differently from the corresponding Go operators.\
    +First, they operate only on basic types (<code>bool</code>, <code>int</code>,\
    +<code>float64</code>, <code>string</code>, etc.).\
    +(Go allows comparison of arrays and structs as well, under some circumstances.)\
    +Second, values can be compared as long as they are the same sort of value:\
    +any signed integer value can be compared to any other signed integer value for example. (Go\
    +does not permit comparing an <code>int8</code> and an <code>int16</code>).\
    +Finally, the <code>eq</code> function (only) allows comparison of the first\
    +argument with one or more following arguments. The template in this example,\
    +</p>\n+\n+<pre>\
    +{{\"{{\"}}if eq .A 1 2 3 {{\"}}\"}} equal {{\"{{\"}}else{{\"}}\"}} not equal {{\"{{\"}}end{{\"}}\"}}\
    +</pre>\n+\n+<p>\n+reports \"equal\" if <code>.A</code> is equal to <em>any</em> of 1, 2, or 3.\
    +</p>\n+\n+<p>\
    +The second change is that a small addition to the grammar makes \"if else if\" chains easier to write.\
    +Instead of writing,\
    +</p>\n+\n+<pre>\
    +{{\"{{\"}}if eq .A 1{{\"}}\"}} X {{\"{{\"}}else{{\"}}\"}} {{\"{{\"}}if eq .A 2{{\"}}\"}} Y {{\"{{\"}}end{{\"}}\"}} {{\"{{\"}}end{{\"}}\"}} \
    +</pre>\n+\n+<p>\
    +one can fold the second \"if\" into the \"else\" and have only one \"end\", like this:\
    +</p>\n+\n+<pre>\
    +{{\"{{\"}}if eq .A 1{{\"}}\"}} X {{\"{{\"}}else if eq .A 2{{\"}}\"}} Y {{\"{{\"}}end{{\"}}\"}}\
    +</pre>\n+\n+<p>\
    +The two forms are identical in effect; the difference is just in the syntax.\
    +</p>\n+\n+<p>\
    +<em>Updating</em>: Neither change affects existing programs. Those that\
    +already define functions called <code>eq</code> and so on through a function\
    +map are unaffected because the associated function map will override the new\
    +default function definitions.\
    +</p>\
     
     <h3 id=\"minor_library_changes\">Minor changes to the library</h3>\
     
    @@ -556,19 +649,19 @@ so that less intermediate buffering is required in general.\
     </li>\
     
     <li>\
    -net: new build tag netgo for building a pure Go net package (CL 7100050).\
    +net: TODO new build tag netgo for building a pure Go net package (CL 7100050).\
     </li>\
     
     <li>\
    -net/http: don\'t allow sending invalid cookie lines (CL 12204043).\
    +net/http: TODO don\'t allow sending invalid cookie lines (CL 12204043).\
     </li>\
     
     <li>\
    -net/http: allow ReadResponse with nil *Request parameter (CL 9821043).\
    +net/http: TODO allow ReadResponse with nil *Request parameter (CL 9821043).\
     </li>\
     
     <li>\
    -net/http: allow responses to HEAD requests, detect type and length (CL 12583043).\
    +net/http: TODO allow responses to HEAD requests, detect type and length (CL 12583043).\
     </li>\
     
     <li>\
    @@ -591,6 +684,21 @@ an <a href=\"/pkg/strings/#IndexByte\"><code>IndexByte</code></a>\
     function for consistency with the <a href=\"/pkg/bytes/\"><code>bytes</code></a> package.\
     </li>\
     
    +<li>\
    +The <a href=\"/pkg/sync/atomic/\"><code>sync/atomic</code></a> package\
    +adds a new set of swap functions that atomically exchange the argument with the\
    +value stored in the pointer, returning the old value.\
    +The functions are\
    +<a href=\"/pkg/sync/atomic/#SwapInt32\"><code>SwapInt32</code></a>,\
    +<a href=\"/pkg/sync/atomic/#SwapInt64\"><code>SwapInt64</code></a>,\
    +<a href=\"/pkg/sync/atomic/#SwapUint32\"><code>SwapUint32</code></a>,\
    +<a href=\"/pkg/sync/atomic/#SwapUint64\"><code>SwapUint64</code></a>,\
    +<a href=\"/pkg/sync/atomic/#SwapUintptr\"><code>SwapUintptr</code></a>,\
    +and\
    +<a href=\"/pkg/sync/atomic/#SwapPointer\"><code>SwapPointer</code></a>,\
    +which swaps an <code>unsafe.Pointer</code>.\
    +</li>\
    +\
     <li>\
     syscall: implemented Sendfile for Darwin, added Syscall9 for Darwin/amd64 (CL 10980043).\
     </li>\
    

コアとなるコードの解説

このコミットは、Go言語のドキュメントを更新するものであり、Go言語のランタイムやライブラリの実装コードそのものを変更するものではありません。したがって、「コアとなるコードの変更箇所」で示されたのは、ドキュメントのHTMLファイルに対する変更です。

これらのHTMLファイルの変更は、Go 1.2で導入された新機能や改善点をユーザーに伝えるためのものです。

  • doc/docs.htmlへの変更は、Go 1.2のリリースノートへのナビゲーションリンクを追加することで、ドキュメント全体の構造とアクセス性を向上させています。これは、ユーザーが最新の変更情報を簡単に見つけられるようにするための重要なUI/UX改善です。
  • doc/go1.2.htmlへの変更は、Go 1.2の主要なライブラリ変更に関する詳細な説明を追加しています。
    • fmtパッケージの引数インデックス機能の説明は、その使い方、導入の背景(国際化)、および既存コードへの影響がないこと(後方互換性)を明確にしています。これにより、開発者はこの新機能を安心して利用できます。
    • text/templateおよびhtml/templateパッケージの変更点(比較関数とelse if構文)の説明は、それぞれの機能の具体的な挙動、Go言語の通常の演算子との違い、そして既存のテンプレートへの影響がないことを詳細に解説しています。特に、eq関数の多引数対応は、テンプレートの記述をより柔軟にするための重要な機能です。
    • sync/atomicパッケージのSwap関数群の説明は、並行処理におけるアトミック操作の重要性と、新しい関数が提供する機能について簡潔に述べています。

これらのドキュメントの更新は、Go言語の進化をユーザーに伝え、新しい機能の採用を促進し、開発者がGo 1.2への移行をスムーズに行えるようにするために不可欠な作業です。

関連リンク

参考にした情報源リンク