4f4a855d82
Reviewed-on: https://go-review.googlesource.com/c/158019 gotools/: * Makefile.am (go_cmd_vet_files): Update for Go1.12beta2 release. (GOTOOLS_TEST_TIMEOUT): Increase to 600. (check-runtime): Export LD_LIBRARY_PATH before computing GOARCH and GOOS. (check-vet): Copy golang.org/x/tools into check-vet-dir. * Makefile.in: Regenerate. gcc/testsuite/: * go.go-torture/execute/names-1.go: Stop using debug/xcoff, which is no longer externally visible. From-SVN: r268084
112 lines
2.1 KiB
Go
112 lines
2.1 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build ignore
|
|
|
|
package ioutil_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func ExampleReadAll() {
|
|
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 ExampleReadDir() {
|
|
files, err := ioutil.ReadDir(".")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, file := range files {
|
|
fmt.Println(file.Name())
|
|
}
|
|
}
|
|
|
|
func ExampleTempDir() {
|
|
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)
|
|
}
|
|
}
|
|
|
|
func ExampleTempFile() {
|
|
content := []byte("temporary file's content")
|
|
tmpfile, err := ioutil.TempFile("", "example")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
if _, err := tmpfile.Write(content); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func ExampleTempFile_suffix() {
|
|
content := []byte("temporary file's content")
|
|
tmpfile, err := ioutil.TempFile("", "example.*.txt")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name()) // clean up
|
|
|
|
if _, err := tmpfile.Write(content); err != nil {
|
|
tmpfile.Close()
|
|
log.Fatal(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func ExampleReadFile() {
|
|
content, err := ioutil.ReadFile("testdata/hello")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Printf("File contents: %s", content)
|
|
|
|
// Output:
|
|
// File contents: Hello, Gophers!
|
|
}
|
|
|
|
func ExampleWriteFile() {
|
|
message := []byte("Hello, Gophers!")
|
|
err := ioutil.WriteFile("testdata/hello", message, 0644)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|