Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334775
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-27 00:02:12

io操作使用例子

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "bytes"
  4.     "fmt"
  5.     "io"
  6.     "io/ioutil"
  7.     "log"
  8.     "os"
  9.     "strings"
  10. )

  11. func ExampleCopy() {
  12.     r := strings.NewReader("some io.Reader stream to be read\n")

  13.     if _, err := io.Copy(os.Stdout, r); err != nil {
  14.         log.Fatal(err)
  15.     }

  16.     // Output:
  17.     // some io.Reader stream to be read
  18. }

  19. func ExampleCopyBuffer() {
  20.     r1 := strings.NewReader("first reader\n")
  21.     r2 := strings.NewReader("second reader\n")
  22.     buf := make([]byte, 8)

  23.     // buf is used here...
  24.     if _, err := io.CopyBuffer(os.Stdout, r1, buf); err != nil {
  25.         log.Fatal(err)
  26.     }

  27.     // ... reused here also. No need to allocate an extra buffer.
  28.     if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
  29.         log.Fatal(err)
  30.     }

  31.     // Output:
  32.     // first reader
  33.     // second reader
  34. }

  35. func ExampleCopyN() {
  36.     r := strings.NewReader("some io.Reader stream to be read")

  37.     if _, err := io.CopyN(os.Stdout, r, 5); err != nil {
  38.         log.Fatal(err)
  39.     }

  40.     // Output:
  41.     // some
  42. }

  43. func ExampleReadAtLeast() {
  44.     r := strings.NewReader("some io.Reader stream to be read\n")

  45.     buf := make([]byte, 33)
  46.     if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
  47.         log.Fatal(err)
  48.     }
  49.     fmt.Printf("%s\n", buf)

  50.     // buffer smaller than minimal read size.
  51.     shortBuf := make([]byte, 3)
  52.     if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
  53.         fmt.Println("error:", err)
  54.     }

  55.     // minimal read size bigger than io.Reader stream
  56.     longBuf := make([]byte, 64)
  57.     if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
  58.         fmt.Println("error:", err)
  59.     }

  60.     // Output:
  61.     // some io.Reader stream to be read
  62.     //
  63.     // error: short buffer
  64.     // error: EOF
  65. }

  66. func ExampleReadFull() {
  67.     r := strings.NewReader("some io.Reader stream to be read\n")

  68.     buf := make([]byte, 4)
  69.     if _, err := io.ReadFull(r, buf); err != nil {
  70.         log.Fatal(err)
  71.     }
  72.     fmt.Printf("%s\n", buf)

  73.     // minimal read size bigger than io.Reader stream
  74.     longBuf := make([]byte, 64)
  75.     if _, err := io.ReadFull(r, longBuf); err != nil {
  76.         fmt.Println("error:", err)
  77.     }

  78.     // Output:
  79.     // some
  80.     // error: unexpected EOF
  81. }

  82. func ExampleWriteString() {
  83.     io.WriteString(os.Stdout, "Hello World")

  84.     // Output: Hello World
  85. }

  86. func ExampleLimitReader() {
  87.     r := strings.NewReader("some io.Reader stream to be read\n")
  88.     lr := io.LimitReader(r, 4)

  89.     if _, err := io.Copy(os.Stdout, lr); err != nil {
  90.         log.Fatal(err)
  91.     }

  92.     // Output:
  93.     // some
  94. }

  95. func ExampleMultiReader() {
  96.     r1 := strings.NewReader("first reader ")
  97.     r2 := strings.NewReader("second reader ")
  98.     r3 := strings.NewReader("third reader\n")
  99.     r := io.MultiReader(r1, r2, r3)

  100.     if _, err := io.Copy(os.Stdout, r); err != nil {
  101.         log.Fatal(err)
  102.     }

  103.     // Output:
  104.     // first reader second reader third reader
  105. }

  106. func ExampleTeeReader() {
  107.     r := strings.NewReader("some io.Reader stream to be read\n")
  108.     var buf bytes.Buffer
  109.     tee := io.TeeReader(r, &buf)

  110.     printall := func(r io.Reader) {
  111.         b, err := ioutil.ReadAll(r)
  112.         if err != nil {
  113.             log.Fatal(err)
  114.         }

  115.         fmt.Printf("%s", b)
  116.     }

  117.     printall(tee)
  118.     printall(&buf)

  119.     // Output:
  120.     // some io.Reader stream to be read
  121.     // some io.Reader stream to be read
  122. }

  123. func ExampleSectionReader() {
  124.     r := strings.NewReader("some io.Reader stream to be read\n")
  125.     s := io.NewSectionReader(r, 5, 17)

  126.     if _, err := io.Copy(os.Stdout, s); err != nil {
  127.         log.Fatal(err)
  128.     }

  129.     // Output:
  130.     // io.Reader stream
  131. }

  132. func ExampleSectionReader_ReadAt() {
  133.     r := strings.NewReader("some io.Reader stream to be read\n")
  134.     s := io.NewSectionReader(r, 5, 16)

  135.     fmt.Println("-----")
  136.     if _, err := io.Copy(os.Stdout, s); err != nil {
  137.         log.Fatal(err)
  138.     }
  139.     fmt.Println()

  140.     buf := make([]byte, 6)
  141.     if _, err := s.ReadAt(buf, 10); err != nil {
  142.         log.Fatal(err)
  143.     }

  144.     fmt.Printf("%s\n", buf)

  145.     // Output:
  146.     // stream
  147. }

  148. func ExampleSectionReader_Seek() {
  149.     r := strings.NewReader("some io.Reader stream to be read\n")
  150.     s := io.NewSectionReader(r, 5, 16)

  151.     if _, err := s.Seek(10, io.SeekStart); err != nil {
  152.         log.Fatal(err)
  153.     }

  154.     buf := make([]byte, 6)
  155.     if _, err := s.Read(buf); err != nil {
  156.         log.Fatal(err)
  157.     }

  158.     fmt.Printf("%s\n", buf)

  159.     // Output:
  160.     // stream
  161. }

  162. func ExampleSeeker_Seek() {
  163.     r := strings.NewReader("some io.Reader stream to be read\n")
  164.     if _, err := io.Copy(os.Stdout, r); err != nil {
  165.         log.Fatal(err)
  166.     }

  167.     r.Seek(15, io.SeekStart)
  168.     if _, err := io.Copy(os.Stdout, r); err != nil {
  169.         log.Fatal(err)
  170.     }

  171.     r.Seek(-5, io.SeekEnd)
  172.     if _, err := io.Copy(os.Stdout, r); err != nil {
  173.         log.Fatal(err)
  174.     }

  175.     // Output:
  176.     // some io.Reader stream to be read
  177.     // stream to be read
  178.     // read
  179. }

  180. func ExampleMultiWriter() {
  181.     r := strings.NewReader("some io.Reader stream to be read\n")

  182.     var buf1, buf2 bytes.Buffer
  183.     w := io.MultiWriter(&buf1, &buf2)

  184.     if _, err := io.Copy(w, r); err != nil {
  185.         log.Fatal(err)
  186.     }

  187.     fmt.Print(buf1.String())
  188.     fmt.Print(buf2.String())

  189.     // Output:
  190.     // some io.Reader stream to be read
  191.     // some io.Reader stream to be read
  192. }

  193. func main() {
  194.     ExampleCopy()
  195.     ExampleCopyBuffer()
  196.     ExampleCopyN()
  197.     ExampleReadAtLeast()
  198.     ExampleReadFull()
  199.     ExampleWriteString()
  200.     fmt.Println()
  201.     ExampleLimitReader()
  202.     ExampleMultiReader()
  203.     ExampleTeeReader()
  204.     ExampleSectionReader()
  205.     fmt.Println()
  206.     ExampleSectionReader_ReadAt()
  207.     ExampleSeeker_Seek()
  208.     ExampleMultiWriter()
  209. }
执行结果:
some io.Reader stream to be read

first reader

second reader

some some io.Reader stream to be read


error: short buffer

error: EOF

some

error: unexpected EOF

Hello World

somefirst reader second reader third reader

some io.Reader stream to be read

some io.Reader stream to be read

io.Reader stream

-----

io.Reader stream

stream

some io.Reader stream to be read

stream to be read

read

some io.Reader stream to be read

some io.Reader stream to be read

成功: 进程退出代码 0.



阅读(353) | 评论(0) | 转发(0) |
0

上一篇:解析命令行参数

下一篇:log操作

给主人留下些什么吧!~~