Home | History | Annotate | Download | only in io
      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 package io_test
      6 
      7 import (
      8 	"bytes"
      9 	"fmt"
     10 	"io"
     11 	"io/ioutil"
     12 	"log"
     13 	"os"
     14 	"strings"
     15 )
     16 
     17 func ExampleCopy() {
     18 	r := strings.NewReader("some io.Reader stream to be read\n")
     19 
     20 	if _, err := io.Copy(os.Stdout, r); err != nil {
     21 		log.Fatal(err)
     22 	}
     23 
     24 	// Output:
     25 	// some io.Reader stream to be read
     26 }
     27 
     28 func ExampleCopyBuffer() {
     29 	r1 := strings.NewReader("first reader\n")
     30 	r2 := strings.NewReader("second reader\n")
     31 	buf := make([]byte, 8)
     32 
     33 	// buf is used here...
     34 	if _, err := io.CopyBuffer(os.Stdout, r1, buf); err != nil {
     35 		log.Fatal(err)
     36 	}
     37 
     38 	// ... reused here also. No need to allocate an extra buffer.
     39 	if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
     40 		log.Fatal(err)
     41 	}
     42 
     43 	// Output:
     44 	// first reader
     45 	// second reader
     46 }
     47 
     48 func ExampleCopyN() {
     49 	r := strings.NewReader("some io.Reader stream to be read")
     50 
     51 	if _, err := io.CopyN(os.Stdout, r, 5); err != nil {
     52 		log.Fatal(err)
     53 	}
     54 
     55 	// Output:
     56 	// some
     57 }
     58 
     59 func ExampleReadAtLeast() {
     60 	r := strings.NewReader("some io.Reader stream to be read\n")
     61 
     62 	buf := make([]byte, 33)
     63 	if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
     64 		log.Fatal(err)
     65 	}
     66 	fmt.Printf("%s\n", buf)
     67 
     68 	// buffer smaller than minimal read size.
     69 	shortBuf := make([]byte, 3)
     70 	if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
     71 		fmt.Println("error:", err)
     72 	}
     73 
     74 	// minimal read size bigger than io.Reader stream
     75 	longBuf := make([]byte, 64)
     76 	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
     77 		fmt.Println("error:", err)
     78 	}
     79 
     80 	// Output:
     81 	// some io.Reader stream to be read
     82 	//
     83 	// error: short buffer
     84 	// error: EOF
     85 }
     86 
     87 func ExampleReadFull() {
     88 	r := strings.NewReader("some io.Reader stream to be read\n")
     89 
     90 	buf := make([]byte, 4)
     91 	if _, err := io.ReadFull(r, buf); err != nil {
     92 		log.Fatal(err)
     93 	}
     94 	fmt.Printf("%s\n", buf)
     95 
     96 	// minimal read size bigger than io.Reader stream
     97 	longBuf := make([]byte, 64)
     98 	if _, err := io.ReadFull(r, longBuf); err != nil {
     99 		fmt.Println("error:", err)
    100 	}
    101 
    102 	// Output:
    103 	// some
    104 	// error: unexpected EOF
    105 }
    106 
    107 func ExampleWriteString() {
    108 	io.WriteString(os.Stdout, "Hello World")
    109 
    110 	// Output: Hello World
    111 }
    112 
    113 func ExampleLimitReader() {
    114 	r := strings.NewReader("some io.Reader stream to be read\n")
    115 	lr := io.LimitReader(r, 4)
    116 
    117 	if _, err := io.Copy(os.Stdout, lr); err != nil {
    118 		log.Fatal(err)
    119 	}
    120 
    121 	// Output:
    122 	// some
    123 }
    124 
    125 func ExampleMultiReader() {
    126 	r1 := strings.NewReader("first reader ")
    127 	r2 := strings.NewReader("second reader ")
    128 	r3 := strings.NewReader("third reader\n")
    129 	r := io.MultiReader(r1, r2, r3)
    130 
    131 	if _, err := io.Copy(os.Stdout, r); err != nil {
    132 		log.Fatal(err)
    133 	}
    134 
    135 	// Output:
    136 	// first reader second reader third reader
    137 }
    138 
    139 func ExampleTeeReader() {
    140 	r := strings.NewReader("some io.Reader stream to be read\n")
    141 	var buf bytes.Buffer
    142 	tee := io.TeeReader(r, &buf)
    143 
    144 	printall := func(r io.Reader) {
    145 		b, err := ioutil.ReadAll(r)
    146 		if err != nil {
    147 			log.Fatal(err)
    148 		}
    149 
    150 		fmt.Printf("%s", b)
    151 	}
    152 
    153 	printall(tee)
    154 	printall(&buf)
    155 
    156 	// Output:
    157 	// some io.Reader stream to be read
    158 	// some io.Reader stream to be read
    159 }
    160 
    161 func ExampleSectionReader() {
    162 	r := strings.NewReader("some io.Reader stream to be read\n")
    163 	s := io.NewSectionReader(r, 5, 17)
    164 
    165 	if _, err := io.Copy(os.Stdout, s); err != nil {
    166 		log.Fatal(err)
    167 	}
    168 
    169 	// Output:
    170 	// io.Reader stream
    171 }
    172 
    173 func ExampleSectionReader_ReadAt() {
    174 	r := strings.NewReader("some io.Reader stream to be read\n")
    175 	s := io.NewSectionReader(r, 5, 16)
    176 
    177 	buf := make([]byte, 6)
    178 	if _, err := s.ReadAt(buf, 10); err != nil {
    179 		log.Fatal(err)
    180 	}
    181 
    182 	fmt.Printf("%s\n", buf)
    183 
    184 	// Output:
    185 	// stream
    186 }
    187 
    188 func ExampleSectionReader_Seek() {
    189 	r := strings.NewReader("some io.Reader stream to be read\n")
    190 	s := io.NewSectionReader(r, 5, 16)
    191 
    192 	if _, err := s.Seek(10, io.SeekStart); err != nil {
    193 		log.Fatal(err)
    194 	}
    195 
    196 	buf := make([]byte, 6)
    197 	if _, err := s.Read(buf); err != nil {
    198 		log.Fatal(err)
    199 	}
    200 
    201 	fmt.Printf("%s\n", buf)
    202 
    203 	// Output:
    204 	// stream
    205 }
    206 
    207 func ExampleMultiWriter() {
    208 	r := strings.NewReader("some io.Reader stream to be read\n")
    209 
    210 	var buf1, buf2 bytes.Buffer
    211 	w := io.MultiWriter(&buf1, &buf2)
    212 
    213 	if _, err := io.Copy(w, r); err != nil {
    214 		log.Fatal(err)
    215 	}
    216 
    217 	fmt.Print(buf1.String())
    218 	fmt.Print(buf2.String())
    219 
    220 	// Output:
    221 	// some io.Reader stream to be read
    222 	// some io.Reader stream to be read
    223 }
    224