...

Source file src/io/example_test.go

Documentation: 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  	"log"
		12  	"os"
		13  	"strings"
		14  )
		15  
		16  func ExampleCopy() {
		17  	r := strings.NewReader("some io.Reader stream to be read\n")
		18  
		19  	if _, err := io.Copy(os.Stdout, r); err != nil {
		20  		log.Fatal(err)
		21  	}
		22  
		23  	// Output:
		24  	// some io.Reader stream to be read
		25  }
		26  
		27  func ExampleCopyBuffer() {
		28  	r1 := strings.NewReader("first reader\n")
		29  	r2 := strings.NewReader("second reader\n")
		30  	buf := make([]byte, 8)
		31  
		32  	// buf is used here...
		33  	if _, err := io.CopyBuffer(os.Stdout, r1, buf); err != nil {
		34  		log.Fatal(err)
		35  	}
		36  
		37  	// ... reused here also. No need to allocate an extra buffer.
		38  	if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
		39  		log.Fatal(err)
		40  	}
		41  
		42  	// Output:
		43  	// first reader
		44  	// second reader
		45  }
		46  
		47  func ExampleCopyN() {
		48  	r := strings.NewReader("some io.Reader stream to be read")
		49  
		50  	if _, err := io.CopyN(os.Stdout, r, 4); err != nil {
		51  		log.Fatal(err)
		52  	}
		53  
		54  	// Output:
		55  	// some
		56  }
		57  
		58  func ExampleReadAtLeast() {
		59  	r := strings.NewReader("some io.Reader stream to be read\n")
		60  
		61  	buf := make([]byte, 14)
		62  	if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
		63  		log.Fatal(err)
		64  	}
		65  	fmt.Printf("%s\n", buf)
		66  
		67  	// buffer smaller than minimal read size.
		68  	shortBuf := make([]byte, 3)
		69  	if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
		70  		fmt.Println("error:", err)
		71  	}
		72  
		73  	// minimal read size bigger than io.Reader stream
		74  	longBuf := make([]byte, 64)
		75  	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
		76  		fmt.Println("error:", err)
		77  	}
		78  
		79  	// Output:
		80  	// some io.Reader
		81  	// error: short buffer
		82  	// error: unexpected EOF
		83  }
		84  
		85  func ExampleReadFull() {
		86  	r := strings.NewReader("some io.Reader stream to be read\n")
		87  
		88  	buf := make([]byte, 4)
		89  	if _, err := io.ReadFull(r, buf); err != nil {
		90  		log.Fatal(err)
		91  	}
		92  	fmt.Printf("%s\n", buf)
		93  
		94  	// minimal read size bigger than io.Reader stream
		95  	longBuf := make([]byte, 64)
		96  	if _, err := io.ReadFull(r, longBuf); err != nil {
		97  		fmt.Println("error:", err)
		98  	}
		99  
	 100  	// Output:
	 101  	// some
	 102  	// error: unexpected EOF
	 103  }
	 104  
	 105  func ExampleWriteString() {
	 106  	if _, err := io.WriteString(os.Stdout, "Hello World"); err != nil {
	 107  		log.Fatal(err)
	 108  	}
	 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  	var r io.Reader = strings.NewReader("some io.Reader stream to be read\n")
	 141  
	 142  	r = io.TeeReader(r, os.Stdout)
	 143  
	 144  	// Everything read from r will be copied to stdout.
	 145  	io.ReadAll(r)
	 146  
	 147  	// Output:
	 148  	// some io.Reader stream to be read
	 149  }
	 150  
	 151  func ExampleSectionReader() {
	 152  	r := strings.NewReader("some io.Reader stream to be read\n")
	 153  	s := io.NewSectionReader(r, 5, 17)
	 154  
	 155  	if _, err := io.Copy(os.Stdout, s); err != nil {
	 156  		log.Fatal(err)
	 157  	}
	 158  
	 159  	// Output:
	 160  	// io.Reader stream
	 161  }
	 162  
	 163  func ExampleSectionReader_ReadAt() {
	 164  	r := strings.NewReader("some io.Reader stream to be read\n")
	 165  	s := io.NewSectionReader(r, 5, 17)
	 166  
	 167  	buf := make([]byte, 6)
	 168  	if _, err := s.ReadAt(buf, 10); err != nil {
	 169  		log.Fatal(err)
	 170  	}
	 171  
	 172  	fmt.Printf("%s\n", buf)
	 173  
	 174  	// Output:
	 175  	// stream
	 176  }
	 177  
	 178  func ExampleSectionReader_Seek() {
	 179  	r := strings.NewReader("some io.Reader stream to be read\n")
	 180  	s := io.NewSectionReader(r, 5, 17)
	 181  
	 182  	if _, err := s.Seek(10, io.SeekStart); err != nil {
	 183  		log.Fatal(err)
	 184  	}
	 185  
	 186  	if _, err := io.Copy(os.Stdout, s); err != nil {
	 187  		log.Fatal(err)
	 188  	}
	 189  
	 190  	// Output:
	 191  	// stream
	 192  }
	 193  
	 194  func ExampleSeeker_Seek() {
	 195  	r := strings.NewReader("some io.Reader stream to be read\n")
	 196  
	 197  	r.Seek(5, io.SeekStart) // move to the 5th char from the start
	 198  	if _, err := io.Copy(os.Stdout, r); err != nil {
	 199  		log.Fatal(err)
	 200  	}
	 201  
	 202  	r.Seek(-5, io.SeekEnd)
	 203  	if _, err := io.Copy(os.Stdout, r); err != nil {
	 204  		log.Fatal(err)
	 205  	}
	 206  
	 207  	// Output:
	 208  	// io.Reader stream to be read
	 209  	// read
	 210  }
	 211  
	 212  func ExampleMultiWriter() {
	 213  	r := strings.NewReader("some io.Reader stream to be read\n")
	 214  
	 215  	var buf1, buf2 bytes.Buffer
	 216  	w := io.MultiWriter(&buf1, &buf2)
	 217  
	 218  	if _, err := io.Copy(w, r); err != nil {
	 219  		log.Fatal(err)
	 220  	}
	 221  
	 222  	fmt.Print(buf1.String())
	 223  	fmt.Print(buf2.String())
	 224  
	 225  	// Output:
	 226  	// some io.Reader stream to be read
	 227  	// some io.Reader stream to be read
	 228  }
	 229  
	 230  func ExamplePipe() {
	 231  	r, w := io.Pipe()
	 232  
	 233  	go func() {
	 234  		fmt.Fprint(w, "some io.Reader stream to be read\n")
	 235  		w.Close()
	 236  	}()
	 237  
	 238  	if _, err := io.Copy(os.Stdout, r); err != nil {
	 239  		log.Fatal(err)
	 240  	}
	 241  
	 242  	// Output:
	 243  	// some io.Reader stream to be read
	 244  }
	 245  
	 246  func ExampleReadAll() {
	 247  	r := strings.NewReader("Go is a general-purpose language designed with systems programming in mind.")
	 248  
	 249  	b, err := io.ReadAll(r)
	 250  	if err != nil {
	 251  		log.Fatal(err)
	 252  	}
	 253  
	 254  	fmt.Printf("%s", b)
	 255  
	 256  	// Output:
	 257  	// Go is a general-purpose language designed with systems programming in mind.
	 258  }
	 259  

View as plain text