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

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

このコミットは、Go言語の標準ライブラリである os パッケージのドキュメンテーションを大幅に強化することを目的としています。具体的には、os パッケージ内の主要な型、関数、定数に対して詳細なコメントを追加し、その機能、引数、戻り値、および使用方法を明確にしています。これにより、os パッケージのAPIがより理解しやすくなり、開発者が効率的に利用できるようになります。

コミット

commit 333cdd8f2ad42ebceaab3a56ee3ca031005983e2
Author: Rob Pike <r@golang.org>
Date:   Sat Mar 7 16:56:44 2009 -0800

    document os
    
    R=rsc
    DELTA=143  (96 added, 0 deleted, 47 changed)
    OCL=25876
    CL=25888

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

https://github.com/golang/go/commit/333cdd8f2ad42ebceaab3a56ee3ca031005983e2

元コミット内容

    document os
    
    R=rsc
    DELTA=143  (96 added, 0 deleted, 47 changed)
    OCL=25876
    CL=25888

変更の背景

このコミットは、Go言語がまだ初期開発段階にあった2009年3月に行われました。Go言語の設計思想の一つに「シンプルさと明瞭さ」があります。標準ライブラリのAPIが直感的で使いやすいことは、言語の普及と開発者の生産性向上に不可欠です。os パッケージは、ファイルシステム操作、プロセス管理、環境変数へのアクセスなど、オペレーティングシステムとの基本的なインタラクションを提供する非常に重要なパッケージです。

初期のコードベースでは、これらの機能に対するドキュメンテーションが不足していた可能性があります。開発者がosパッケージを効果的に利用するためには、各関数や型の役割、引数の意味、戻り値の挙動、そして発生しうるエラーについての明確な説明が求められます。このコミットは、Go言語の主要な設計者の一人であるRob Pikeによって行われており、Go言語の初期段階からドキュメンテーションの重要性が認識されていたことを示しています。APIの安定化と並行して、その利用方法を明確にすることが、ライブラリの品質向上とエコシステムの健全な成長に寄与するという背景があります。

前提知識の解説

このコミットの変更内容を理解するためには、以下の前提知識が役立ちます。

  • Go言語の基本的な構文: 関数定義、変数宣言、構造体(struct)、定数(const)、パッケージ(package)、インポート(import)など、Go言語の基本的なコード構造を理解している必要があります。
  • Go言語のドキュメンテーション規約: Go言語では、エクスポートされた(大文字で始まる)型、関数、変数、定数には、その直前にコメントを記述することでドキュメンテーションとして扱われます。このコメントは godoc ツールによって自動的に解析され、APIドキュメントとして生成されます。コメントは、その要素の目的、引数、戻り値、エラー条件などを簡潔かつ明確に説明する必要があります。
  • os パッケージの役割: os パッケージは、Goプログラムがオペレーティングシステムと対話するための基本的な機能を提供します。これには、ファイル操作(開く、読み書き、閉じる)、ディレクトリ操作(作成、読み取り)、プロセス管理(実行、待機)、環境変数へのアクセス、システム時刻の取得などが含まれます。
  • Unixシステムコール: os パッケージの多くの機能は、内部的にUnix系のシステムコール(syscallパッケージを介して)を呼び出しています。例えば、ファイルのオープンには syscall.Open、プロセスのフォークには syscall.ForkExec などが使われます。このコミットで追加されたドキュメンテーションは、これらの低レベルなシステムコールがGoのAPIとしてどのように抽象化されているかを説明しています。
  • ファイルディスクリプタ (File Descriptor, FD): Unix系システムにおいて、開かれたファイルやソケット、パイプなどのI/Oリソースは、整数値のファイルディスクリプタによって識別されます。os パッケージの FD 型は、このファイルディスクリプタをGoのオブジェクトとしてラップしたものです。
  • エラーハンドリング: Go言語では、エラーは通常、関数の最後の戻り値として error 型(またはこのコミット当時の *Error 型)で返されます。このコミットでは、各関数のドキュメンテーションにエラーが返される条件が明記されています。

技術的詳細

このコミットの技術的詳細は、主にGo言語のドキュメンテーション規約に則ったコメントの追加と、それに伴うコードの微調整にあります。

  1. コメントの追加と改善:

    • os パッケージ全体にわたって、エクスポートされたすべての型、関数、定数に詳細なコメントが追加されています。これらのコメントは、godoc ツールがAPIドキュメントを生成する際に利用されます。
    • コメントは、その要素の目的、引数の意味、戻り値、エラー条件、そして場合によっては具体的な使用例や注意点(例: Exec 関数が成功時に戻らないこと、ForkExec が推奨されること)を明確に記述しています。
    • 特に os/file.go では、FD 型の役割、Stdin, Stdout, Stderr の説明、Open 関数のフラグ(O_RDONLY など)の詳細な説明が追加されています。
    • os/error.go では、Error 型がシングルトン構造であること、NewErrorErrnoToError の役割、そして一般的なUnixエラー定数について説明されています。
    • os/exec.go では、ForkExecExec の違い、Waitmsg 構造体のフィールド、Wait 関数のオプションなどが詳細に記述されています。
    • os/types.go では、Dir 構造体の各フィールドの意味、および IsFifo, IsChar などのファイルタイプ判定メソッドの役割が明確化されています。
  2. コード構造の微調整:

    • src/lib/os/env.go では、ENOENV 変数の宣言が var (...) ブロックから独立した var 宣言に変更され、その直前にコメントが配置されています。これは、godoc がコメントを正しく関連付けるための一般的なパターンです。
    • src/lib/os/file.goOpen 関数では、引数名が modeflags から flagperm に変更されています。これは、syscall.Open の引数名とより一致させ、意味を明確にするための変更であり、ドキュメンテーションの改善と合わせて行われています。
    • src/lib/os/file.goPipe 関数では、戻り値の変数名が fd1, fd2 から r, w に変更され、読み取り側と書き込み側がより直感的にわかるようになっています。

これらの変更は、Go言語のAPI設計における「自己文書化」の原則を強く反映しています。コード自体がその機能を説明するよう努め、必要に応じて詳細なコメントで補足することで、外部ドキュメントに依存することなく、コードベースから直接情報を得られるようにしています。

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

このコミットは、主に既存のコードへのコメント追加と、それに伴う微細なコード構造の調整が中心です。以下に、各ファイルにおける主要な変更箇所を抜粋します。

src/lib/os/env.go

--- a/src/lib/os/env.go
+++ b/src/lib/os/env.go
@@ -11,11 +11,11 @@ import (
 	"os";
 )
 
-var (
-	ENOENV = NewError("no such environment variable");
+// ENOENV is the Error indicating that an environment variable does not exist.
+var ENOENV = NewError("no such environment variable");
+
+var env map[string] string;
 
-	env map[string] string;
-)
 
 func copyenv() {
 	env = make(map[string] string);
@@ -29,6 +29,8 @@ func copyenv() {
 	}
 }
 
+// Getenv retrieves the value of the environment variable named by the key.
+// It returns the value and an error, if any.
 func Getenv(key string) (value string, err *Error) {
 	once.Do(copyenv);
 
@@ -42,6 +44,8 @@ func Getenv(key string) (value string, err *Error) {
 	return v, nil;
 }
 
+// Setenv sets the value of the environment variable named by the key.
+// It returns an Error, if any.
 func Setenv(key, value string) *Error {
 	once.Do(copyenv);
 
@@ -52,11 +56,14 @@ func Setenv(key, value string) *Error {
 	return nil;
 }
 
+// Clearenv deletes all environment variables.
 func Clearenv() {
 	once.Do(copyenv);	// prevent copyenv in Getenv/Setenv
 	env = make(map[string] string);
 }
 
+// Environ returns an array of strings representing the environment,
+// in the form "key=value".
 func Environ() []string {
 	once.Do(copyenv);
 	a := make([]string, len(env));

src/lib/os/error.go

--- a/src/lib/os/error.go
+++ b/src/lib/os/error.go
@@ -6,8 +6,12 @@ package os
 
 import syscall "syscall"
 
-// Errors are singleton structures. Use the String() method to get their contents --
-// it handles the nil (no error) case.
+// Error is a structure wrapping a string describing an error.
+// Errors are singleton structures, created by NewError, so their addresses can
+// be compared to test for equality. A nil Error pointer means ``no error''.
+// Use the String() method to get the contents; it handles the nil case.
+// The Error type is intended for use by any package that wishes to define
+// error strings.
 type Error struct {
 	s string
 }
@@ -24,7 +28,8 @@ var errorStringTab = make(map[string] *Error);
 // These functions contain a race if two goroutines add identical
 // errors simultaneously but the consequences are unimportant.
 
-// Allocate an Error object, but if it's been seen before, share that one.
+// NewError allocates an Error object, but if s has been seen before,
+// shares the Error associated with that message.
 func NewError(s string) *Error {
 	if s == "" {
 		return nil
@@ -38,7 +43,8 @@ func NewError(s string) *Error {
 	return err;
 }
 
-// Allocate an Error objecct, but if it's been seen before, share that one.
+// ErrnoToError calls NewError to create an Error object for the string
+// associated with Unix error code errno.
 func ErrnoToError(errno int64) *Error {
 	if errno == 0 {
 		return nil
@@ -53,6 +59,7 @@ func ErrnoToError(errno int64) *Error {
 	return err;
 }
 
+// Commonly known Unix errors.
 var (
 	ENONE = ErrnoToError(syscall.ENONE);
 	EPERM = ErrnoToError(syscall.EPERM);
@@ -92,6 +99,7 @@ var (
 	EAGAIN = ErrnoToError(syscall.EAGAIN);
 )
 
+// String returns the string associated with the Error.
 func (e *Error) String() string {
 	if e == nil {
 		return "No Error"

src/lib/os/exec.go

--- a/src/lib/os/exec.go
+++ b/src/lib/os/exec.go
@@ -9,6 +9,12 @@ import (
 	"syscall";
 )
 
+// ForkExec forks the current process and invokes Exec with the file, arguments,
+// and environment specified by argv0, argv, and envv.  It returns the process
+// id of the forked process and an Error, if any.  The fd array specifies the
+// file descriptors to be set up in the new process: fd[0] will be Unix file
+// descriptor 0 (standard input), fd[1] descriptor 1, and so on.  A nil entry
+// will cause the child to have no open file descriptor with that index.
 func ForkExec(argv0 string, argv []string, envv []string, fd []*FD)
 	(pid int, err *Error)
 {
@@ -26,6 +32,10 @@ func ForkExec(argv0 string, argv []string, envv []string, fd []*FD)
 	return int(p), ErrnoToError(e);
 }
 
+// Exec replaces the current process with an execution of the program
+// named by argv0, with arguments argv and environment envv.
+// If successful, Exec never returns.  If it fails, it returns an Error.
+// ForkExec is almost always a better way to execute a program.
 func Exec(argv0 string, argv []string, envv []string) *Error {
 	if envv == nil {
 		envv = Environ();
@@ -41,18 +51,24 @@ func Exec(argv0 string, argv []string, envv []string) *Error {
 // since syscall one might have different field types across
 // different OS.
 
+// Waitmsg stores the information about an exited process as reported by Wait.
 type Waitmsg struct {
-\tPid int;\
-\tsyscall.WaitStatus;\
-\tRusage *syscall.Rusage;\
+\tPid int;	// The process's id.
+\tsyscall.WaitStatus;	// System-dependent status info.
+\tRusage *syscall.Rusage;	// System-dependent resource usage info.
 }\
 
+// Options for Wait.
 const (
-\tWNOHANG = syscall.WNOHANG;\
-\tWSTOPPED = syscall.WSTOPPED;\
-\tWRUSAGE = 1<<60;\
+\tWNOHANG = syscall.WNOHANG;	// Don't wait if no process has exited.
+\tWSTOPPED = syscall.WSTOPPED;	// If set, status of stopped subprocesses is also reported.
+\tWUNTRACED = WSTOPPED;
+\tWRUSAGE = 1<<60;	// Record resource usage.
 )\
 
+// Wait waits for process pid to exit or stop, and then returns a
+// Waitmsg describing its status and an Error, if any. The options
+// (WNOHANG etc.) affect the behavior of the Wait call.
 func Wait(pid int, options uint64) (w *Waitmsg, err *Error) {
 	var status syscall.WaitStatus;
 	var rusage *syscall.Rusage;

src/lib/os/file.go

--- a/src/lib/os/file.go
+++ b/src/lib/os/file.go
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// The os package provides a platform-independent interface to operating
+// system functionality.  The design is Unix-like.
 package os
 
 import (
@@ -16,21 +18,25 @@ type dirInfo struct {	// TODO(r): 6g bug means this can't be private
 	bufp	int64;	// location of next record in buf.
 }
 
-// FDs are wrappers for file descriptors
+// FD represents an open file.
+// TODO(r): is FD the right name? Would File be better?
 type FD struct {
 	fd int64;
 	name	string;
 	dirinfo	*dirInfo;	// nil unless directory being read
 }
 
+// Fd returns the integer Unix file descriptor referencing the open file.
 func (fd *FD) Fd() int64 {
 	return fd.fd
 }
 
+// Name returns the name of the file as presented to Open.
 func (fd *FD) Name() string {
 	return fd.name
 }
 
+// NewFD returns a new FD with the given file descriptor and name.
 func NewFD(fd int64, name string) *FD {
 	if fd < 0 {
 		return nil
@@ -38,28 +44,35 @@ func NewFD(fd int64, name string) *FD {
 	return &FD{fd, name, nil}
 }
 
+// Stdin, Stdout, and Stderr are open FDs pointing to the standard input,
+// standard output, and standard error file descriptors.
 var (
 	Stdin = NewFD(0, "/dev/stdin");
 	Stdout = NewFD(1, "/dev/stdout");
 	Stderr = NewFD(2, "/dev/stderr");
 )
 
+// Flags to Open wrapping those of the underlying system. Not all flags
+// may be implemented on a given system.
 const (
-\tO_RDONLY = syscall.O_RDONLY;
-\tO_WRONLY = syscall.O_WRONLY;
-\tO_RDWR = syscall.O_RDWR;
-\tO_APPEND = syscall.O_APPEND;
-\tO_ASYNC = syscall.O_ASYNC;
-\tO_CREAT = syscall.O_CREAT;
-\tO_NOCTTY = syscall.O_NOCTTY;
-\tO_NONBLOCK = syscall.O_NONBLOCK;
-\tO_NDELAY = O_NONBLOCK;
-\tO_SYNC = syscall.O_SYNC;
-\tO_TRUNC = syscall.O_TRUNC;
+\tO_RDONLY = syscall.O_RDONLY;	// open the file read-only.
+\tO_WRONLY = syscall.O_WRONLY;	// open the file write-only.
+\tO_RDWR = syscall.O_RDWR;	// open the file read-write.
+\tO_APPEND = syscall.O_APPEND;	// open the file append-only.
+\tO_ASYNC = syscall.O_ASYNC;	// generate a signal when I/O is available.
+\tO_CREAT = syscall.O_CREAT;	// create a new file if none exists.
+\tO_NOCTTY = syscall.O_NOCTTY;	// do not make file the controlling tty.
+\tO_NONBLOCK = syscall.O_NONBLOCK;	// open in non-blocking mode.
+\tO_NDELAY = O_NONBLOCK;		// synonym for O_NONBLOCK
+\tO_SYNC = syscall.O_SYNC;	// open for synchronous I/O.
+\tO_TRUNC = syscall.O_TRUNC;	// if possible, truncate file when opened.
 )\
 
-func Open(name string, mode int, flags int) (fd *FD, err *Error) {
-\tr, e := syscall.Open(name, int64(mode), int64(flags | syscall.O_CLOEXEC));
+// Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.)
+// if applicable.  If successful, methods on the returned FD can be used for I/O.
+// It returns the FD and an Error, if any.
+func Open(name string, flag int, perm int) (fd *FD, err *Error) {
+\tr, e := syscall.Open(name, int64(flag | syscall.O_CLOEXEC), int64(perm));
 \tif e != 0 {
 \t\treturn nil, ErrnoToError(e);
 \t}\
@@ -73,6 +86,8 @@ func Open(name string, mode int, flags int) (fd *FD, err *Error) {
 \treturn NewFD(r, name), ErrnoToError(e)
 }
 
+// Close closes the FD, rendering it unusable for I/O.
+// It returns an Error, if any.
 func (fd *FD) Close() *Error {
 	if fd == nil {
 		return EINVAL
@@ -82,6 +97,10 @@ func (fd *FD) Close() *Error {
 	return ErrnoToError(e)
 }
 
+// Read reads up to len(b) bytes from the FD.
+// It returns the number of bytes read and an Error, if any.
+// EOF is signaled by a zero count with a nil Error.
+// TODO(r): Add Pread, Pwrite (maybe ReadAt, WriteAt).
 func (fd *FD) Read(b []byte) (ret int, err *Error) {
 	if fd == nil {
 		return 0, EINVAL
@@ -96,6 +115,9 @@ func (fd *FD) Read(b []byte) (ret int, err *Error) {
 	return int(r), ErrnoToError(e)
 }
 
+// Write writes len(b) bytes to the FD.
+// It returns the number of bytes written and an Error, if any.
+// If the byte count differs from len(b), it usually implies an error occurred.
 func (fd *FD) Write(b []byte) (ret int, err *Error) {
 	if fd == nil {
 		return 0, EINVAL
@@ -110,6 +132,10 @@ func (fd *FD) Write(b []byte) (ret int, err *Error) {
 	return int(r), ErrnoToError(e)
 }
 
+// Seek sets the offset for the next Read or Write on FD to offset, interpreted
+// according to whence: 0 means relative to the origin of the file, 1 means
+// relative to the current offset, and 2 means relative to the end.
+// It returns the new offset and an Error, if any.
 func (fd *FD) Seek(offset int64, whence int) (ret int64, err *Error) {
 	r, e := syscall.Seek(fd.fd, offset, int64(whence));
 	if e != 0 {
@@ -121,6 +147,8 @@ func (fd *FD) Seek(offset int64, whence int) (ret int64, err *Error) {
 	return r, nil
 }
 
+// WriteString is like Write, but writes the contents of string s rather than
+// an array of bytes.
 func (fd *FD) WriteString(s string) (ret int, err *Error) {
 	if fd == nil {
 		return 0, EINVAL
@@ -132,12 +160,14 @@ func (fd *FD) WriteString(s string) (ret int, err *Error) {
 	return int(r), ErrnoToError(e)
 }
 
-func Pipe() (fd1 *FD, fd2 *FD, err *Error) {
+// Pipe returns a connected pair of FDs; reads from r return bytes written to w.
+// It returns the FDs and an Error, if any.
+func Pipe() (r *FD, w *FD, err *Error) {
 	var p [2]int64;
 
 	// See ../syscall/exec.go for description of lock.
 	syscall.ForkLock.RLock();
-\tr, e := syscall.Pipe(&p);
+\tret, e := syscall.Pipe(&p);
 	if e != 0 {
 		syscall.ForkLock.RUnlock();
 		return nil, nil, ErrnoToError(e)
@@ -149,11 +179,17 @@ func Pipe() (fd1 *FD, fd2 *FD, err *Error) {
 	return NewFD(p[0], "|0"), NewFD(p[1], "|1"), nil
 }
 
+// Mkdir creates a new directory with the specified name and permission bits.
+// It returns an error, if any.
 func Mkdir(name string, perm int) *Error {
 	r, e := syscall.Mkdir(name, int64(perm));
 	return ErrnoToError(e)
 }
 
+// Stat returns the Dir structure describing the named file. If the file
+// is a symbolic link, it returns information about the file the link
+// references.
+// It returns the Dir and an error, if any.
 func Stat(name string) (dir *Dir, err *Error) {
 	stat := new(syscall.Stat_t);
 	r, e := syscall.Stat(name, stat);
@@ -163,6 +199,8 @@ func Stat(name string) (dir *Dir, err *Error) {
 	return dirFromStat(name, new(Dir), stat), nil
 }
 
+// Fstat returns the Dir structure describing the file associated with the FD.
+// It returns the Dir and an error, if any.
 func Fstat(fd *FD) (dir *Dir, err *Error) {
 	stat := new(syscall.Stat_t);
 	r, e := syscall.Fstat(fd.fd, stat);
@@ -172,6 +210,9 @@ func Fstat(fd *FD) (dir *Dir, err *Error) {
 	return dirFromStat(fd.name, new(Dir), stat), nil
 }
 
+// Lstat returns the Dir structure describing the named file. If the file
+// is a symbolic link, it returns information about the link itself.
+// It returns the Dir and an error, if any.
 func Lstat(name string) (dir *Dir, err *Error) {
 	stat := new(syscall.Stat_t);
 	r, e := syscall.Lstat(name, stat);
@@ -181,10 +222,21 @@ func Lstat(name string) (dir *Dir, err *Error) {
 	return dirFromStat(name, new(Dir), stat), nil
 }
 
-// Non-portable function defined in operating-system-dependent file.
+// Readdirnames has a non-portable implemenation so its code is separated into an
+// operating-system-dependent file.
+\
+// Readdirnames reads the contents of the directory associated with fd and
+// returns an array of up to count names, in directory order.  Subsequent
+// calls on the same fd will yield further names.
+// A negative count means to read until EOF.
+// It returns the array and an Error, if any.
 func Readdirnames(fd *FD, count int) (names []string, err *os.Error)
 
-// Negative count means read until EOF.
+// Readdir reads the contents of the directory associated with fd and
+// returns an array of up to count Dir structures, in directory order.  Subsequent
+// calls on the same fd will yield further Dirs.
+// A negative count means to read until EOF.
+// It returns the array and an Error, if any.
 func Readdir(fd *FD, count int) (dirs []Dir, err *os.Error) {
 	dirname := fd.name;
 	if dirname == "" {

src/lib/os/time.go

--- a/src/lib/os/time.go
+++ b/src/lib/os/time.go
@@ -9,6 +9,11 @@ import (
 	"syscall"
 )
 
+\
+// Time returns the current time, in whole seconds and
+// fractional nanoseconds, plus an Error if any. The current
+// time is thus 1e9*sec+nsec, in nanoseconds.  The zero of
+// time is the Unix epoch.
 func Time() (sec int64, nsec int64, err *Error) {
 	var errno int64;
 	sec, nsec, errno = syscall.Gettimeofday();

src/lib/os/types.go

--- a/src/lib/os/types.go
+++ b/src/lib/os/types.go
@@ -9,52 +9,60 @@ import "syscall"
 // An operating-system independent representation of Unix data structures.
 // OS-specific routines in this directory convert the OS-local versions to these.
 
-// Result of stat64(2) etc.
+// A Dir describes a file and is returned by Stat, Fstat, and Lstat
 type Dir struct {
-\tDev	uint64;
-\tIno	uint64;
-\tNlink	uint64;
-\tMode	uint32;
-\tUid	uint32;
-\tGid	uint32;
-\tRdev	uint64;
-\tSize	uint64;
-\tBlksize	uint64;
-\tBlocks	uint64;
-\tAtime_ns	uint64;	// nanoseconds since 1970
-\tMtime_ns	uint64;	// nanoseconds since 1970
-\tCtime_ns	uint64;	// nanoseconds since 1970
-\tName	string;
+\tDev	uint64;	// device number of file system holding file.
+\tIno	uint64;	// inode number.
+\tNlink	uint64;	// number of hard links.
+\tMode	uint32;	// permission and mode bits.
+\tUid	uint32;	// user id of owner.
+\tGid	uint32;	// group id of owner.
+\tRdev	uint64;	// device type for special file.
+\tSize	uint64;	// length in bytes.
+\tBlksize	uint64;	// size of blocks, in bytes.
+\tBlocks	uint64;	// number of blocks allocated for file.
+\tAtime_ns	uint64;	// access time; nanoseconds since epoch.
+\tMtime_ns	uint64;	// modified time; nanoseconds since epoch.
+\tCtime_ns	uint64;	// status change time; nanoseconds since epoch.
+\tName	string;	// name of file as presented to Open.
 }\
 
+// IsFifo reports whether the Dir describes a FIFO file.
 func (dir *Dir) IsFifo() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFIFO
 }\
 
+// IsChar reports whether the Dir describes a character special file.
 func (dir *Dir) IsChar() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFCHR
 }\
 
+// IsDirectory reports whether the Dir describes a directory.
 func (dir *Dir) IsDirectory() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFDIR
 }\
 
+// IsBlock reports whether the Dir describes a block special file.
 func (dir *Dir) IsBlock() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFBLK
 }\
 
+// IsRegular reports whether the Dir describes a regular file.
 func (dir *Dir) IsRegular() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFREG
 }\
 
+// IsSymlink reports whether the Dir describes a symbolic link.
 func (dir *Dir) IsSymlink() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFLNK
 }\
 
+// IsSocket reports whether the Dir describes a socket.
 func (dir *Dir) IsSocket() bool {
 	return (dir.Mode & syscall.S_IFMT) == syscall.S_IFSOCK
 }\
 
+// Permission returns the file permission bits.
 func (dir *Dir) Permission() int {
 	return int(dir.Mode & 0777)
 }\

コアとなるコードの解説

このコミットの「コアとなるコードの変更」は、実質的にはGo言語のドキュメンテーション規約に沿ったコメントの追加そのものです。Go言語では、エクスポートされた識別子(関数、型、変数など)の直前に記述されたコメントが、その識別子の公式なドキュメンテーションとして扱われます。

具体的には、以下のようなパターンでドキュメンテーションが追加されています。

  1. パッケージレベルのコメント: src/lib/os/file.go の冒頭に os パッケージ全体の目的を説明するコメントが追加されています。

    // The os package provides a platform-independent interface to operating
    // system functionality.  The design is Unix-like.
    package os
    

    これは、パッケージの概要を理解するための最初の入り口となります。

  2. 型定義へのコメント: FDDir のような構造体定義の直前に、その型が何を表すのか、どのような情報を含むのかを説明するコメントが追加されています。

    // FD represents an open file.
    // TODO(r): is FD the right name? Would File be better?
    type FD struct {
        fd int64;
        name    string;
        dirinfo *dirInfo;    // nil unless directory being read
    }
    

    Dir 構造体の各フィールドにも詳細なコメントが追加され、それぞれのフィールドがどのような情報を保持しているかが明確になっています。

  3. 関数・メソッドへのコメント: Getenv, Setenv, Open, Read, Write, ForkExec, Wait など、主要な関数やメソッドの直前に、その機能、引数、戻り値、エラー条件などを詳細に説明するコメントが追加されています。

    // Getenv retrieves the value of the environment variable named by the key.
    // It returns the value and an error, if any.
    func Getenv(key string) (value string, err *Error) { ... }
    

    特に ForkExecOpen のように引数が多い、あるいは挙動が複雑な関数については、その詳細な説明が開発者の理解を深める上で非常に重要です。

  4. 定数へのコメント: O_RDONLYWNOHANG のような定数群に対しても、それぞれの定数が何を表すのか、どのような目的で使用されるのかがコメントで説明されています。

    const (
        O_RDONLY = syscall.O_RDONLY;    // open the file read-only.
        O_WRONLY = syscall.O_WRONLY;    // open the file write-only.
        // ...
    )
    

これらのコメントは、Go言語の godoc ツールによって自動的に解析され、HTML形式のAPIドキュメントとして提供されます。これにより、開発者はIDEの補完機能や go doc コマンド、またはGoの公式ドキュメントサイトを通じて、これらの詳細な説明に簡単にアクセスできるようになります。

このコミットは、機能追加やバグ修正ではなく、既存のAPIの「使いやすさ」と「理解しやすさ」を向上させるための、純粋な品質改善のコミットであると言えます。Go言語の初期段階からこのようなドキュメンテーションへの注力がなされていたことは、その後のGoエコシステムの成長において、標準ライブラリの高品質なAPIドキュメントが重要な役割を果たしたことを示唆しています。

関連リンク

  • Go言語の os パッケージの現在のドキュメント: https://pkg.go.dev/os (このコミット当時のものではありませんが、現在のAPIの理解に役立ちます)
  • Go言語のドキュメンテーションの書き方: https://go.dev/blog/godoc

参考にした情報源リンク

  • Go言語の公式ドキュメント: https://go.dev/
  • Go言語のソースコード (GitHub): https://github.com/golang/go
  • Go言語の初期の歴史に関する記事や講演 (Rob Pike, Ken Thompson, Robert Griesemerによるもの)
  • Unix系オペレーティングシステムのシステムコールに関する一般的な情報源 (例: manページ)
  • コミットハッシュ 333cdd8f2ad42ebceaab3a56ee3ca031005983e2 のGitHubコミットページ
  • ./commit_data/1782.txt のコミット情報