1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build ignore
6
7package ioutil_test
8
9import (
10	"fmt"
11	"io/ioutil"
12	"log"
13	"os"
14	"path/filepath"
15	"strings"
16)
17
18func ExampleReadAll() {
19	r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
20
21	b, err := ioutil.ReadAll(r)
22	if err != nil {
23		log.Fatal(err)
24	}
25
26	fmt.Printf("%s", b)
27
28	// Output:
29	// Go is a general-purpose language designed with systems programming in mind.
30}
31
32func ExampleReadDir() {
33	files, err := ioutil.ReadDir(".")
34	if err != nil {
35		log.Fatal(err)
36	}
37
38	for _, file := range files {
39		fmt.Println(file.Name())
40	}
41}
42
43func ExampleTempDir() {
44	content := []byte("temporary file's content")
45	dir, err := ioutil.TempDir("", "example")
46	if err != nil {
47		log.Fatal(err)
48	}
49
50	defer os.RemoveAll(dir) // clean up
51
52	tmpfn := filepath.Join(dir, "tmpfile")
53	if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
54		log.Fatal(err)
55	}
56}
57
58func ExampleTempFile() {
59	content := []byte("temporary file's content")
60	tmpfile, err := ioutil.TempFile("", "example")
61	if err != nil {
62		log.Fatal(err)
63	}
64
65	defer os.Remove(tmpfile.Name()) // clean up
66
67	if _, err := tmpfile.Write(content); err != nil {
68		log.Fatal(err)
69	}
70	if err := tmpfile.Close(); err != nil {
71		log.Fatal(err)
72	}
73}
74
75func ExampleReadFile() {
76	content, err := ioutil.ReadFile("testdata/hello")
77	if err != nil {
78		log.Fatal(err)
79	}
80
81	fmt.Printf("File contents: %s", content)
82
83	// Output:
84	// File contents: Hello, Gophers!
85}
86