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

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

このコミットは、Go言語の標準ライブラリosパッケージ内のGetwd関数の戻り値の変数名を、pwdからdirに変更するものです。この変更は、pwdという略語が「present working directory(現在の作業ディレクトリ)」を意味する一方で、「passwd(パスワード)」と誤解される可能性を排除し、コードの可読性と明確性を向上させることを目的としています。機能的な変更は一切含まれていません。

コミット

commit 4b9ab7dd69453186a0e08757ba177b5d3ec25e7e
Author: Shenghou Ma <minux.ma@gmail.com>
Date:   Thu Apr 17 23:17:15 2014 -0400

    os: change return variable name for Getwd to avoid confusion
    changed (pwd string) to (dir string), as some think pwd means passwd.
    Fixes #7811.
    
    LGTM=iant
    R=golang-codereviews, iant, bradfitz
    CC=golang-codereviews
    https://golang.org/cl/89100043

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

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

元コミット内容

os: change return variable name for Getwd to avoid confusion
changed (pwd string) to (dir string), as some think pwd means passwd.
Fixes #7811.

LGTM=iant
R=golang-codereviews, iant, bradfitz
CC=golang-codereviews
https://golang.org/cl/89100043

変更の背景

この変更の主な背景は、os.Getwd関数が返す現在の作業ディレクトリを表す変数名pwdが、一部のユーザーにとって「passwd」(パスワード)と誤解される可能性があったためです。特にUnix系システムに慣れている開発者にとって、pwdは「print working directory」コマンドの略語として広く認識されていますが、一般的な文脈では「passwd」という単語も存在するため、混乱を招くことがありました。

Go言語は、コードの明確性と可読性を非常に重視しています。このような潜在的な誤解を避けるため、より直感的で曖昧さのないdirという変数名に変更されました。コミットメッセージにあるFixes #7811は、この問題がGoの旧来の課題追跡システムで報告されていたことを示唆しています。

前提知識の解説

os.Getwd関数

os.GetwdはGo言語の標準ライブラリosパッケージに含まれる関数で、現在の作業ディレクトリの絶対パスを文字列として返します。この関数は、プログラムがファイルシステム上のどの位置で実行されているかを知る必要がある場合に頻繁に使用されます。

PWD (Present Working Directory)

PWDは、Unix系オペレーティングシステムにおいて、現在の作業ディレクトリ(カレントディレクトリ)を示す環境変数です。シェルコマンドのpwd(print working directory)も、この現在の作業ディレクトリを表示するために使用されます。開発者にとって非常に基本的な概念であり、ファイルパスの解決やスクリプトの実行において重要な役割を果たします。

passwd

passwdは、Unix系システムにおいてユーザーのパスワード情報を管理するためのファイル(/etc/passwd)や、パスワードを変更するためのコマンドを指します。pwdと発音が似ていること、そしてどちらもシステム管理に関連する用語であることから、混同される可能性がありました。

Go言語における変数名の重要性

Go言語の設計思想の一つに、コードのシンプルさと明確性があります。変数名や関数名は、その目的や内容を明確に伝えるべきであるとされています。この原則に従い、たとえ小さな曖牲でも、誤解の余地がある命名は避けるべきとされています。今回の変更は、このGo言語の設計哲学を反映したものです。

技術的詳細

このコミットによる技術的な変更は、os.Getwd関数のシグネチャと、その関数内で使用されるローカル変数およびキャッシュ変数の名前をpwdからdirに一括して変更した点に集約されます。

具体的には、以下の変更が行われました。

  1. 関数の戻り値の変数名変更: func Getwd() (pwd string, err error) から func Getwd() (dir string, err error) へ変更。
  2. 関数内部での変数名の変更: pwdという名前で参照されていたすべてのローカル変数や、getwdCache構造体内のdirフィールド(以前はpwdとして扱われていた可能性が高い)への参照がdirに変更されました。

この変更は純粋に命名規則に関するものであり、os.Getwd関数の動作や機能、パフォーマンスには一切影響を与えません。コンパイル後のバイナリサイズや実行速度にも変化はありません。これは、コードのセマンティクス(意味)を変えることなく、その表現をより明確にするためのリファクタリングの一種です。

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

変更はsrc/pkg/os/getwd.goファイル内で行われました。主要な変更箇所は以下の通りです。

--- a/src/pkg/os/getwd.go
+++ b/src/pkg/os/getwd.go
@@ -22,7 +22,7 @@ var useSyscallwd = func(error) bool { return true }\n // current directory.  If the current directory can be\n // reached via multiple paths (due to symbolic links),\n // Getwd may return any one of them.\n-func Getwd() (pwd string, err error) {\n+func Getwd() (dir string, err error) {\n  // If the operating system provides a Getwd call, use it.\n  if syscall.ImplementsGetwd {\n  s, e := syscall.Getwd()\n@@ -39,22 +39,22 @@ func Getwd() (pwd string, err error) {\n \n  // Clumsy but widespread kludge:\n  // if $PWD is set and matches \".\", use it.\n-\tpwd = Getenv(\"PWD\")\n-\tif len(pwd) > 0 && pwd[0] == \'/\' {\n-\t\td, err := Stat(pwd)\n+\tdir = Getenv(\"PWD\")\n+\tif len(dir) > 0 && dir[0] == \'/\' {\n+\t\td, err := Stat(dir)\n  \tif err == nil && SameFile(dot, d) {\n-\t\t\treturn pwd, nil\n+\t\t\treturn dir, nil\n  \t}\n  }\n \n  // Apply same kludge but to cached dir instead of $PWD.\n  getwdCache.Lock()\n-\tpwd = getwdCache.dir\n+\tdir = getwdCache.dir\n  getwdCache.Unlock()\n-\tif len(pwd) > 0 {\n-\t\td, err := Stat(pwd)\n+\tif len(dir) > 0 {\n+\t\td, err := Stat(dir)\n  \tif err == nil && SameFile(dot, d) {\n-\t\t\treturn pwd, nil\n+\t\t\treturn dir, nil\n  \t}\n  }\n \n@@ -71,8 +71,8 @@ func Getwd() (pwd string, err error) {\n \n  // General algorithm: find name in parent\n  // and then find name of parent.  Each iteration\n-\t// adds /name to the beginning of pwd.\n-\tpwd = \"\"\n+\t// adds /name to the beginning of dir.\n+\tdir = \"\"\n  for parent := \"..\"; ; parent = \"../\" + parent {\n  if len(parent) >= 1024 { // Sanity check\n  return \"\", syscall.ENAMETOOLONG\n@@ -91,7 +91,7 @@ func Getwd() (pwd string, err error) {\n  \t\t\tfor _, name := range names {\n  \t\t\t\td, _ := Lstat(parent + \"/\" + name)\n  \t\t\t\tif SameFile(d, dot) {\n-\t\t\t\t\tpwd = \"/\" + name + pwd\n+\t\t\t\t\tdir = \"/\" + name + dir\n  \t\t\t\t\tgoto Found\n  \t\t\t\t}\n  \t\t\t}\n@@ -112,8 +112,8 @@ func Getwd() (pwd string, err error) {\n \n  // Save answer as hint to avoid the expensive path next time.\n  getwdCache.Lock()\n-\tgetwdCache.dir = pwd\n+\tgetwdCache.dir = dir\n  getwdCache.Unlock()\n \n-\treturn pwd, nil\n+\treturn dir, nil\n }\n```

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

上記の差分が示すように、`Getwd`関数の定義において、戻り値の変数名が`pwd`から`dir`に変更されています。これに伴い、関数本体内で`pwd`という名前で参照されていたすべての箇所が`dir`に置き換えられています。

例えば、以下の行が変更されています。

*   `func Getwd() (pwd string, err error) {` が `func Getwd() (dir string, err error) {` に。
*   `pwd = Getenv("PWD")` が `dir = Getenv("PWD")` に。
*   `return pwd, nil` が `return dir, nil` に。

この変更は、Go言語のコードベース全体における命名の一貫性と明確性を高めるための典型的なリファクタリングです。開発者が`os.Getwd`の戻り値を見たときに、それが「現在のディレクトリ」を指すことがより明確になり、誤解の可能性が排除されます。

## 関連リンク

*   **Go Gerrit Change-ID**: `https://golang.org/cl/89100043` (Goプロジェクトが以前使用していたコードレビューシステムGerritの変更履歴)

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

*   コミットメッセージの内容
*   Go言語の`os`パッケージに関する一般的な知識
*   Unix系システムの`pwd`コマンドと`passwd`の概念