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

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

このコミットは、Go言語の標準ライブラリであるstrconvパッケージから削除された古いAPI関数(Atob, AtoF{64,32}, Ftoa)への参照を、関連するドキュメントやテストコードから修正するものです。これにより、Go言語のAPI変更に合わせたドキュメントとコードの整合性が保たれます。

コミット

commit 4cfa9e3c616ee9ffd688adc7caffa76bfd7ee312
Author: Shenghou Ma <minux.ma@gmail.com>
Date:   Sat Jan 14 10:59:45 2012 -0800

    doc: fix comments referring to removed API funcs
            The strconv package has removed Atob, AtoF{64,32} and Ftoa.
    
    R=golang-dev, r
    CC=golang-dev
    https://golang.org/cl/5540057
---
 src/pkg/exp/sql/driver/types.go | 2 +-\
 src/pkg/fmt/doc.go              | 5 +++--
 src/pkg/math/all_test.go        | 2 +-\
 src/pkg/strconv/fp_test.go      | 8 ++++----\
 4 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/pkg/exp/sql/driver/types.go b/src/pkg/exp/sql/driver/types.go
index 0ee278856d..d6ba641cb2 100644
--- a/src/pkg/exp/sql/driver/types.go
+++ b/src/pkg/exp/sql/driver/types.go
@@ -40,7 +40,7 @@ type ValueConverter interface {
 //       1 is true
 //       0 is false,
 //       other integers are an error
-//  - for strings and []byte, same rules as strconv.Atob
+//  - for strings and []byte, same rules as strconv.ParseBool
 //  - all other types are an error
 var Bool boolType
 
diff --git a/src/pkg/fmt/doc.go b/src/pkg/fmt/doc.go
index 11e9f19f89..7d4178da76 100644
--- a/src/pkg/fmt/doc.go
+++ b/src/pkg/fmt/doc.go
@@ -30,8 +30,9 @@
  		%X	base 16, with upper-case letters for A-F
  		%U	Unicode format: U+1234; same as "U+%04X"
  	Floating-point and complex constituents:
-\t\t%b\tdecimalless scientific notation with exponent a power
-\t\t\tof two, in the manner of strconv.Ftoa32, e.g. -123456p-78
+\t\t%b\tdecimalless scientific notation with exponent a power of two, 
+\t\t\tin the manner of strconv.FormatFloat with the 'b' format, 
+\t\t\te.g. -123456p-78
  		%e	scientific notation, e.g. -1234.456e+78
  		%E	scientific notation, e.g. -1234.456E+78
  		%f	decimal point but no exponent, e.g. 123.456
diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go
index 2f73c06272..ed66a42fb0 100644
--- a/src/pkg/math/all_test.go
+++ b/src/pkg/math/all_test.go
@@ -2536,7 +2536,7 @@ func TestLargeTan(t *testing.T) {
 }
 
 // Check that math constants are accepted by compiler
-// and have right value (assumes strconv.Atof works).
+// and have right value (assumes strconv.ParseFloat works).
 // http://code.google.com/p/go/issues/detail?id=201
 
 type floatTest struct {
diff --git a/src/pkg/strconv/fp_test.go b/src/pkg/strconv/fp_test.go
index 47877e373a..171defa441 100644
--- a/src/pkg/strconv/fp_test.go
+++ b/src/pkg/strconv/fp_test.go
@@ -26,8 +26,8 @@ func pow2(i int) float64 {
 	return pow2(i/2) * pow2(i-i/2)
 }
 
-// Wrapper around strconv.Atof64.  Handles dddddp+ddd (binary exponent)
-// itself, passes the rest on to strconv.Atof64.
+// Wrapper around strconv.ParseFloat(x, 64).  Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
 func myatof64(s string) (f float64, ok bool) {
 	a := strings.SplitN(s, "p", 2)
 	if len(a) == 2 {
@@ -70,8 +70,8 @@ func myatof64(s string) (f float64, ok bool) {
 	return f1, true
 }
 
-// Wrapper around strconv.Atof32.  Handles dddddp+ddd (binary exponent)
-// itself, passes the rest on to strconv.Atof32.
+// Wrapper around strconv.ParseFloat(x, 32).  Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
 func myatof32(s string) (f float32, ok bool) {
 	a := strings.SplitN(s, "p", 2)
 	if len(a) == 2 {

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

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

元コミット内容

doc: fix comments referring to removed API funcs
        The strconv package has removed Atob, AtoF{64,32} and Ftoa.

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

変更の背景

このコミットの主な背景は、Go言語の標準ライブラリであるstrconvパッケージにおけるAPIの変更です。具体的には、Atob(文字列から真偽値への変換)、AtoF{64,32}(文字列から浮動小数点数への変換)、Ftoa(浮動小数点数から文字列への変換)といった古い関数が削除されました。

これらの関数が削除されたのは、Go言語のAPI設計が進化し、より汎用的で一貫性のある関数が導入されたためと考えられます。例えば、AtobParseBoolに、AtoF{64,32}ParseFloatに、FtoaFormatFloatにそれぞれ置き換えられました。新しい関数は、より柔軟な引数(例えば、ParseFloatはビットサイズを引数で指定できる)や、より広範なフォーマットオプションを提供することで、開発者にとって使いやすく、かつ堅牢な機能を提供します。

このコミットは、これらのAPI変更に伴い、Go言語のコードベース内の既存のドキュメントやテストコードが、削除された古い関数を参照し続けている箇所を特定し、新しい関数への参照に更新することで、コードベース全体の整合性と正確性を保つことを目的としています。

前提知識の解説

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

  • strconvパッケージ: Go言語の標準ライブラリの一つで、"string conversion"(文字列変換)の略です。このパッケージは、文字列と基本的なデータ型(真偽値、整数、浮動小数点数など)の間で相互に変換を行うための機能を提供します。例えば、数値の文字列表現を実際の数値型に変換したり、その逆を行ったりする際に使用されます。

  • Atob (ASCII to Boolean): strconvパッケージに以前存在した関数で、文字列を真偽値(bool型)に変換するために使用されました。例えば、"true"や"false"といった文字列をtruefalseという真偽値に変換します。

  • AtoF{64,32} (ASCII to Float): strconvパッケージに以前存在した関数で、文字列を浮動小数点数(float64またはfloat32型)に変換するために使用されました。AtoF64は64ビット浮動小数点数に、AtoF32は32ビット浮動小数点数に変換します。

  • Ftoa (Float to ASCII): strconvパッケージに以前存在した関数で、浮動小数点数(float64またはfloat32型)を文字列に変換するために使用されました。

  • ParseBool: strconvパッケージの現在の関数で、Atobの後継です。文字列を真偽値にパースします。より多くの真偽値の文字列表現(例: "true", "false", "T", "F", "1", "0"など)を認識できます。

  • ParseFloat: strconvパッケージの現在の関数で、AtoF{64,32}の後継です。文字列を浮動小数点数にパースします。第2引数でビットサイズ(32または64)を指定することで、float32またはfloat64としてパースできます。

  • FormatFloat: strconvパッケージの現在の関数で、Ftoaの後継です。浮動小数点数を指定されたフォーマット(精度、表記法など)で文字列に変換します。

  • fmtパッケージ: Go言語の標準ライブラリの一つで、"formatted I/O"(フォーマットされた入出力)の略です。PrintfSprintfなどの関数を提供し、様々なデータ型を整形して標準出力、ファイル、または文字列に出力するために使用されます。

  • %bフォーマット動詞 (in fmt package): fmtパッケージで浮動小数点数をフォーマットする際に使用される動詞の一つです。これは、浮動小数点数を「2のべき乗を指数とする小数点なしの科学表記」(decimalless scientific notation with exponent a power of two)で表現します。例えば、-123456p-78のような形式です。これは、IEEE 754浮動小数点数のバイナリ表現を直接的に示す際に有用です。

技術的詳細

このコミットの技術的な核心は、Go言語のstrconvパッケージにおけるAPIの近代化と、それに伴うコードベース全体の整合性維持です。

Go言語の開発チームは、時間の経過とともにAPIを改善し、より堅牢で使いやすいものに進化させています。このコミットで修正されているAtob, AtoF{64,32}, Ftoaといった関数は、初期のGo言語で提供されていたものですが、より汎用性が高く、エラーハンドリングやフォーマットオプションが強化されたParseBool, ParseFloat, FormatFloatといった関数に置き換えられました。

  • AtobからParseBoolへの移行: Atobは単純な真偽値変換を提供していましたが、ParseBoolはより多くの真偽値の文字列表現(例: "t", "f", "T", "F", "TRUE", "FALSE", "True", "False", "1", "0")を適切に処理できるようになりました。これにより、入力の多様性に対応し、より堅牢なコードを書くことが可能になります。

  • AtoF{64,32}からParseFloatへの移行: AtoF64AtoF32はそれぞれ特定のビットサイズの浮動小数点数に変換する関数でしたが、ParseFloatは単一の関数で、第2引数にビットサイズ(32または64)を指定することで、float32またはfloat64のいずれかとしてパースできるようになりました。これにより、APIの重複が解消され、より統一的なインターフェースが提供されます。また、ParseFloatは、指数表記、無限大(Inf)、非数(NaN)といった特殊な浮動小数点数表現も正確にパースできます。

  • FtoaからFormatFloatへの移行: Ftoaは浮動小数点数を文字列に変換する基本的な機能を提供していましたが、FormatFloatは、フォーマットの形式('f', 'e', 'g', 'x', 'b'など)、精度、ビットサイズといった詳細なオプションを指定できるようになりました。これにより、開発者は浮動小数点数の文字列表現をより細かく制御できるようになります。

このコミットで行われている変更は、主にコメントとテストコードの更新です。これは、古いAPIを参照している箇所を新しいAPIに修正することで、ドキュメントが実際のコードの動作と一致し、テストが最新のAPIを使用して正しく機能することを保証するためです。このような変更は、大規模なコードベースにおいてAPIの進化を管理し、開発者が常に正確な情報に基づいて作業できるようにするために不可欠です。

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

このコミットでは、以下の4つのファイルが変更されています。

  1. src/pkg/exp/sql/driver/types.go
  2. src/pkg/fmt/doc.go
  3. src/pkg/math/all_test.go
  4. src/pkg/strconv/fp_test.go

これらの変更はすべて、削除されたstrconvパッケージのAPI関数への参照を、新しい代替関数への参照に置き換えるものです。

コアとなるコードの解説

src/pkg/exp/sql/driver/types.go

--- a/src/pkg/exp/sql/driver/types.go
+++ b/src/pkg/exp/sql/driver/types.go
@@ -40,7 +40,7 @@ type ValueConverter interface {
 //       1 is true
 //       0 is false,
 //       other integers are an error
-//  - for strings and []byte, same rules as strconv.Atob
+//  - for strings and []byte, same rules as strconv.ParseBool
 //  - all other types are an error
 var Bool boolType

この変更は、database/sql/driverパッケージ内のValueConverterインターフェースのBool型に関するコメントを更新しています。以前は、文字列や[]byteから真偽値への変換ルールがstrconv.Atobと同じであると記述されていました。しかし、Atobstrconvパッケージから削除されたため、その記述を新しいstrconv.ParseBoolに言及するように修正しました。これは、APIの変更に伴うドキュメントの整合性を維持するための、純粋なコメントの修正です。

src/pkg/fmt/doc.go

--- a/src/pkg/fmt/doc.go
+++ b/src/pkg/fmt/doc.go
@@ -30,8 +30,9 @@
  		%X	base 16, with upper-case letters for A-F
  		%U	Unicode format: U+1234; same as "U+%04X"
  	Floating-point and complex constituents:
-\t\t%b\tdecimalless scientific notation with exponent a power
-\t\t\tof two, in the manner of strconv.Ftoa32, e.g. -123456p-78
+\t\t%b\tdecimalless scientific notation with exponent a power of two, 
+\t\t\tin the manner of strconv.FormatFloat with the 'b' format, 
+\t\t\te.g. -123456p-78
  		%e	scientific notation, e.g. -1234.456e+78
  		%E	scientific notation, e.g. -1234.456E+78
  		%f	decimal point but no exponent, e.g. 123.456

この変更は、fmtパッケージのドキュメントにおける%bフォーマット動詞の説明を更新しています。以前は、このフォーマットがstrconv.Ftoa32の形式に準拠していると記述されていました。しかし、Ftoa32が削除され、より汎用的なstrconv.FormatFloatに統合されたため、その説明をstrconv.FormatFloat'b'フォーマットを使用する形式であると修正しました。これもドキュメントの正確性を保つための重要な修正です。

src/pkg/math/all_test.go

--- a/src/pkg/math/all_test.go
+++ b/src/pkg/math/all_test.go
@@ -2536,7 +2536,7 @@ func TestLargeTan(t *testing.T) {
 }
 
 // Check that math constants are accepted by compiler
-// and have right value (assumes strconv.Atof works).
+// and have right value (assumes strconv.ParseFloat works).
 // http://code.google.com/p/go/issues/detail?id=201
 
 type floatTest struct {

この変更は、mathパッケージのテストファイル内のコメントを更新しています。数学定数がコンパイラに受け入れられ、正しい値を持つことを確認するテストに関するコメントで、以前はstrconv.Atofが機能することを前提としていました。AtoFが削除されParseFloatに置き換えられたため、その前提をstrconv.ParseFloatが機能することに変更しました。これは、テストの意図を正確に反映させるためのコメント修正です。

src/pkg/strconv/fp_test.go

--- a/src/pkg/strconv/fp_test.go
+++ b/src/pkg/strconv/fp_test.go
@@ -26,8 +26,8 @@ func pow2(i int) float64 {
 	return pow2(i/2) * pow2(i-i/2)
 }
 
-// Wrapper around strconv.Atof64.  Handles dddddp+ddd (binary exponent)
-// itself, passes the rest on to strconv.Atof64.
+// Wrapper around strconv.ParseFloat(x, 64).  Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
 func myatof64(s string) (f float64, ok bool) {
 	a := strings.SplitN(s, "p", 2)
 	if len(a) == 2 {
@@ -70,8 +70,8 @@ func myatof64(s string) (f float64, ok bool) {
 	return f1, true
 }
 
-// Wrapper around strconv.Atof32.  Handles dddddp+ddd (binary exponent)
-// itself, passes the rest on to strconv.Atof32.
+// Wrapper around strconv.ParseFloat(x, 32).  Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
 func myatof32(s string) (f float32, ok bool) {
 	a := strings.SplitN(s, "p", 2)
 	if len(a) == 2 {

この変更は、strconvパッケージの浮動小数点数テストファイル内のmyatof64myatof32というヘルパー関数のコメントを更新しています。これらの関数は、バイナリ指数表記を処理し、残りの部分をstrconvの浮動小数点数パース関数に渡すラッパーです。以前はstrconv.Atof64strconv.Atof32をラップしていると記述されていましたが、API変更によりstrconv.ParseFloat(x, 64)strconv.ParseFloat(x, 32)をラップしていると修正されました。これは、テストコードが新しいAPIを使用するように内部的に更新されたことを示唆しており、コメントもそれに合わせて修正されています。

関連リンク

参考にした情報源リンク

  • Go言語公式ドキュメント (strconvパッケージ, fmtパッケージ)
  • IEEE 754浮動小数点数標準に関する一般的な知識
  • Go言語のAPI設計と進化に関する一般的な知識