Documentation
¶
Overview ¶
ioutilパッケージは、いくつかのI/Oユーティリティ関数を実装しています。
Deprecated: Go 1.16以降、同じ機能はパッケージ io またはパッケージ os で提供されるようになり、 これらの実装が新しいコードで優先されるべきです。 詳細については、特定の関数のドキュメントを参照してください。
Index ¶
- Variables
- func NopCloser(r io.Reader) io.ReadCloserdeprecated
- func ReadAll(r io.Reader) ([]byte, error)deprecated
- func ReadDir(dirname string) ([]fs.FileInfo, error)deprecated
- func ReadFile(filename string) ([]byte, error)deprecated
- func TempDir(dir, pattern string) (name string, err error)deprecated
- func TempFile(dir, pattern string) (f *os.File, err error)deprecated
- func WriteFile(filename string, data []byte, perm fs.FileMode) errordeprecated
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Discard io.Writer = io.Discard
Discardは、何もしないですべての書き込み呼び出しが成功するio.Writerです。
Deprecated: Go 1.16以降、この値は単に io.Discard です。
Functions ¶
func NopCloser
deprecated
func NopCloser(r io.Reader) io.ReadCloser
NopCloserは、提供されたReader rをラップするCloseメソッドのないReadCloserを返します。
Deprecated: Go 1.16以降、この関数は単に io.NopCloser を呼び出すだけです。
func ReadAll
deprecated
ReadAllは、rからエラーまたはEOFが発生するまで読み取り、読み取ったデータを返します。 成功した呼び出しはerr == nilを返します。err == EOFではありません。 ReadAllは、EOFをエラーとして報告する必要はありません。 なぜなら、ReadAllはsrcからEOFまで読み取るように定義されているためです。
Deprecated: Go 1.16以降、この関数は単に io.ReadAll を呼び出すだけです。
Example ¶
package main
import (
"github.com/shogo82148/std/fmt"
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
"github.com/shogo82148/std/strings"
)
func main() {
r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
b, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", b)
}
Output: Go is a general-purpose language designed with systems programming in mind.
func ReadDir
deprecated
ReadDirは、dirnameで指定されたディレクトリを読み取り、 ファイル名でソートされたディレクトリの内容の fs.FileInfo リストを返します。 ディレクトリの読み取り中にエラーが発生した場合、 ReadDirはエラーとともにディレクトリエントリを返しません。
Deprecated: Go 1.16以降、 os.ReadDir がより効率的で正確な選択肢となります。 os.ReadDir は fs.FileInfo のリストではなく[fs.DirEntry]のリストを返し、 ディレクトリの読み取り中にエラーが発生した場合でも部分的な結果を返します。
fs.FileInfo のリストを引き続き取得する必要がある場合は、次のようにします。
entries, err := os.ReadDir(dirname)
if err != nil { ... }
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil { ... }
infos = append(infos, info)
}
Example ¶
package main
import (
"github.com/shogo82148/std/fmt"
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
)
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name())
}
}
Output:
func ReadFile
deprecated
ReadFileは、filenameで指定されたファイルを読み取り、その内容を返します。 成功した呼び出しはerr == nilを返します。err == EOFではありません。 ReadFileは、ファイル全体を読み取るため、ReadからのEOFをエラーとして報告する必要はありません。
Deprecated: Go 1.16以降、この関数は単に os.ReadFile を呼び出すだけです。
Example ¶
package main
import (
"github.com/shogo82148/std/fmt"
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
)
func main() {
content, err := ioutil.ReadFile("testdata/hello")
if err != nil {
log.Fatal(err)
}
fmt.Printf("File contents: %s", content)
}
Output: File contents: Hello, Gophers!
func TempDir
deprecated
TempDirは、ディレクトリdirに新しい一時ディレクトリを作成し、 ディレクトリ名を生成するためにpatternを取り、ランダムな文字列を末尾に追加します。 patternに"*"が含まれている場合、ランダムな文字列が最後の"*"に置き換えられます。 TempDirは、新しいディレクトリの名前を返します。 dirが空の文字列の場合、TempDirは一時ファイルのデフォルトディレクトリを使用します(os.TempDir を参照)。 同時にTempDirを呼び出す複数のプログラムは、同じディレクトリを選択しません。 呼び出し元は、ディレクトリが不要になったら削除する責任があります。
Deprecated: Go 1.17以降、この関数は単に os.MkdirTemp を呼び出すだけです。
Example ¶
package main
import (
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
"github.com/shogo82148/std/os"
"github.com/shogo82148/std/path/filepath"
)
func main() {
content := []byte("temporary file's content")
dir, err := ioutil.TempDir("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
tmpfn := filepath.Join(dir, "tmpfile")
if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
log.Fatal(err)
}
}
Output:
Example (Suffix) ¶
package main
import (
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
"github.com/shogo82148/std/os"
"github.com/shogo82148/std/path/filepath"
)
func main() {
parentDir := os.TempDir()
logsDir, err := ioutil.TempDir(parentDir, "*-logs")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(logsDir) // クリーンアップ
// ログは、*-logsで終わるサフィックスを持つすべてのディレクトリを検索することで、必要に応じて早期にクリーンアップできます。
globPattern := filepath.Join(parentDir, "*-logs")
matches, err := filepath.Glob(globPattern)
if err != nil {
log.Fatalf("%qのマッチングに失敗しました: %v", globPattern, err)
}
for _, match := range matches {
if err := os.RemoveAll(match); err != nil {
log.Printf("%qの削除に失敗しました: %v", match, err)
}
}
}
Output:
func TempFile
deprecated
TempFileは、ディレクトリdirに新しい一時ファイルを作成し、 ファイルを読み書きするために開き、結果の *os.File を返します。 ファイル名は、patternを取り、ランダムな文字列を末尾に追加して生成されます。 patternに"*"が含まれている場合、ランダムな文字列が最後の"*"に置き換えられます。 dirが空の文字列の場合、TempFileは一時ファイルのデフォルトディレクトリを使用します(os.TempDir を参照)。 同時にTempFileを呼び出す複数のプログラムは、同じファイルを選択しません。 呼び出し元は、f.Name()を使用してファイルのパス名を見つけることができます。 ファイルが不要になったら、呼び出し元の責任でファイルを削除する必要があります。
Deprecated: Go 1.17以降、この関数は単に os.CreateTemp を呼び出すだけです。
Example ¶
package main
import (
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
"github.com/shogo82148/std/os"
)
func main() {
content := []byte("temporary file's content")
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // クリーンアップ
if _, err := tmpfile.Write(content); err != nil {
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
}
Output:
Example (Suffix) ¶
package main
import (
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
"github.com/shogo82148/std/os"
)
func main() {
content := []byte("temporary file's content")
tmpfile, err := ioutil.TempFile("", "example.*.txt")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // クリーンアップ
if _, err := tmpfile.Write(content); err != nil {
tmpfile.Close()
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
}
Output:
func WriteFile
deprecated
WriteFileは、filenameで指定されたファイルにデータを書き込みます。 ファイルが存在しない場合、WriteFileは、パーミッションperm(umaskの前)で作成します。 それ以外の場合、WriteFileはパーミッションを変更せずに書き込むために切り捨てます。
Deprecated: Go 1.16以降、この関数は単に os.WriteFile を呼び出すだけです。
Example ¶
package main
import (
"github.com/shogo82148/std/io/ioutil"
"github.com/shogo82148/std/log"
)
func main() {
message := []byte("Hello, Gophers!")
err := ioutil.WriteFile("hello", message, 0644)
if err != nil {
log.Fatal(err)
}
}
Output:
Types ¶
This section is empty.