23 September 2017

Table of Contents

variables

var Discard io.Writer = devNull(0)

func NopCloser(r io.Reader) io.ReadCloser

req.Body = ioutil.NopCloser(bytes.NewReader(b))

func ReadAll(r io.Reader) ([]byte, error)

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)

func ReadDir(dirname string) ([]os.FileInfo, error)

files, err := ioutil.ReadDir(".")
if err != nil {
      log.Fatal(err)
}

for _, file := range files {
      fmt.Println(file.Name())
}

func ReadFile(filename string) ([]byte, error)

content, err := ioutil.ReadFile("testdata/hello")
if err != nil {
      log.Fatal(err)
}

fmt.Printf("File contents: %s", content)

func TempDir(dir, prefix string) (name string, err error)

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 TempFile(dir, prefix string) (f *os.File, err error)

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 WriteFile(filename string, data []byte, perm os.FileMode) error