欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GO語言的IO方法實例小結(jié)

 更新時間:2015年10月27日 15:55:17   投稿:goldensun  
這篇文章主要介紹了GO語言的IO方法實例小結(jié),Docker的火爆促成了當(dāng)下新興的Go語言人氣的大幅攀升,需要的朋友可以參考下

type PipeWriter

復(fù)制代碼 代碼如下:

type PipeWriter struct {
    // contains filtered or unexported fields
}

(1)func (w *PipeWriter) Close() error關(guān)閉管道,關(guān)閉時正在進行的Read操作將返回EOF,若管道內(nèi)仍有未讀取的數(shù)據(jù),后續(xù)仍可正常讀取
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 go w.Write([]byte("hello word"))

 data := make([]byte, 10)
 n, err := r.Read(data)
 w.Close()
 if err == io.EOF {
  fmt.Println("executing read return EOF")
  fmt.Println("executing read reads number", n)
 }
 n, _ = r.Read(data)
 fmt.Println(string(data))          //hello word
 fmt.Println("next read number", n) //next read number 0
}


(2)func (w *PipeWriter) CloseWithError(err error) error這個函數(shù)和read里邊的CloseWithError是大同小異的,關(guān)閉管道,關(guān)閉時正在進行的Read操作將返回參數(shù)傳入的異常,若管道內(nèi)仍有未讀取的數(shù)據(jù),后續(xù)仍可正常讀取
復(fù)制代碼 代碼如下:

import (
 "errors"
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 go w.Write([]byte("hello widuu"))
 newerr := errors.New("your daye 突然關(guān)閉了")
 w.CloseWithError(newerr)
 data := make([]byte, 10)
 _, err := r.Read(data)
 if err != nil {
  fmt.Println(err) //your daye 突然關(guān)閉了
 }
}


(3)func (w *PipeWriter) Write(data []byte) (n int, err error)終于來打write了,這個是把字節(jié)切片寫入管道,返回的是寫入字節(jié)數(shù)和error,前邊用到的太多了,隨便哪一個吧
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 go w.Write([]byte("hello widuu")) //寫入的是[]byte,注意官方文檔寫的是,寫入管道阻塞,一直到所有數(shù)據(jù)的讀取結(jié)束
 data := make([]byte, 11)
 n, _ := r.Read(data)
 fmt.Println(string(data))     //hello widuu
 fmt.Println("read number", n) //read number 10
}


type Reader

復(fù)制代碼 代碼如下:

type Reader interface {
    Read(p []byte) (n int, err error)
}

(1)func LimitReader(r Reader, n int64) Reader,我們之前就說了Reader這個結(jié)構(gòu),其實這就是對Reader的一次封裝,限定了它讀取字節(jié)數(shù),其實他實現(xiàn)的就是io.LimitedReader{}這個結(jié)構(gòu)
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
 "reflect"
)

func main() {
 f, _ := os.Open("test.txt")
 defer f.Close()
 reader := io.LimitReader(f, 5)
 p := make([]byte, 5)
 fmt.Println(reflect.TypeOf(reader)) //*io.LimitedReader
 var total int
 for {
  n, err := reader.Read(p)
  if err == io.EOF {
   fmt.Println("read value", string(p[:total])) //read value hello
   fmt.Println(total)                           //5
   break
  }
  total = total + n
 }

}


(2)func MultiReader(readers ...Reader) Reader這個函數(shù)一看就知道是封裝了多個readers,跟上邊的方法差不多,只是封裝了多個而已,當(dāng)然還去除了讀取的限制,我們代碼給大家測試一下
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
 "reflect"
)

func main() {
 f1, _ := os.Open("test1.txt")
 f2, _ := os.Open("test.txt")
 defer f1.Close()
 defer f2.Close()
 reader := io.MultiReader(f1, f2) //*io.multiReader
 fmt.Println(reflect.TypeOf(reader))
 p := make([]byte, 10)
 var total int
 var data string
 for {
  n, err := reader.Read(p)
  if err == io.EOF {
   fmt.Println("read end", total) //read end 17
   break
  }
  total = total + n
  data = data + string(p[:n])
 }
 fmt.Println("read value", data)  //read value widuu2hello widuu
 fmt.Println("read count", total) // read count 17
}


(3)既然上邊介紹讀了,我這介紹個寫吧type Write`func MultiWriter(writers ...Writer) Writer一樣的作用只不過是這次換成寫了
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "io/ioutil"
 "os"
)

func main() {
 f1, _ := os.Create("1.txt")
 f2, _ := os.Create("2.txt")
 writer := io.MultiWriter(f1, f2)
 writer.Write([]byte("widuu"))
 //千萬別這么邏輯來 ,我這是測試用的哈
 r1, _ := ioutil.ReadFile("1.txt")
 r2, _ := ioutil.ReadFile("2.txt")
 fmt.Println(string(r1)) //widuu
 fmt.Println(string(r2)) //widuu
}


(4)func TeeReader(r Reader, w Writer) Reader這個方法有意思是從r中讀取數(shù)據(jù)然后寫入到w中,這個沒有內(nèi)部緩沖區(qū),看下代碼
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
 "reflect"
)

func main() {
 r, _ := os.Open("test.txt")
 w, _ := os.Create("test2.txt")
 reader := io.TeeReader(r, w)
 fmt.Println(reflect.TypeOf(reader)) //*io.teeReader
 p := make([]byte, 10)
 n, _ := reader.Read(p)
 fmt.Println(string(p[:n])) //hello widu
}


type SectionReader{}

復(fù)制代碼 代碼如下:

type SectionReader struct {
    // contains filtered or unexported fields
}


(1)func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader,你一看就知道了,其實就是通過這個方法獲取到io.SectionReader,第一個參數(shù)讀取器,第二個參數(shù)偏移量,第三個參數(shù)是讀取多少

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
 "reflect"
)

func main() {
 f, _ := os.Open("test.txt")
 sr := io.NewSectionReader(f, 2, 5)
 fmt.Println(reflect.TypeOf(sr)) //*io.SectionReader
}


(2)func (s *SectionReader) Read(p []byte) (n int, err error)熟悉的read()其實就是讀取數(shù)據(jù)用的,大家看函數(shù)就可以理解了,因為咱們經(jīng)常遇到這個上兩個都寫這個了~~
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 f, _ := os.Open("test.txt")
 defer f.Close()
 sr := io.NewSectionReader(f, 2, 5)
 p := make([]byte, 10)
 n, err := sr.Read(p)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(string(p[:n])) //llo w
}


(3)func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)額這個跟之前的ReadAt是一樣的,只不過只有一個偏移量,少了截取數(shù),但是你要知道SectionReader做的是什么就把數(shù)據(jù)截取了,所以就不需要截取數(shù)了
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 f, _ := os.Open("test.txt")
 defer f.Close()
 sr := io.NewSectionReader(f, 2, 5)
 p := make([]byte, 10)
 n, err := sr.ReadAt(p, 1)
 if err == io.EOF {
  fmt.Println(string(p[:n])) // lo w
 }

}


(4)func (s *SectionReader) Seek(offset int64, whence int) (int64, error)這個是設(shè)置文件指針的便宜量的,之前我們的os里邊也是有個seek的,對SectionReader的讀取起始點、當(dāng)前讀取點、結(jié)束點進行偏移,offset 偏移量,whence 設(shè)定選項 0:讀取起始點,1:當(dāng)前讀取點,2:結(jié)束點(不好用),其他:將拋出Seek: invalid whence異常
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 f, _ := os.Open("test.txt")
 defer f.Close()
 sr := io.NewSectionReader(f, 2, 5)
 p := make([]byte, 10)
 sr.Seek(1, 0)      //相當(dāng)于起始的地址偏移1
 n, err := sr.Read(p)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(string(p[:n])) //lo w 是不是達(dá)到了前邊的ReadAt()
}


(5)func (s *SectionReader) Size() int64返回的是可以讀取的字節(jié)數(shù),這個不受偏移指針的影響,也不受當(dāng)前讀取的影響,我們具體看下代碼
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 f, _ := os.Open("test.txt")
 defer f.Close()
 sr := io.NewSectionReader(f, 2, 5)
 fmt.Println(sr.Size()) //5
 p := make([]byte, 10)
 sr.Seek(1, 0)    //相當(dāng)于起始的地址偏移1
 n, err := sr.Read(p)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(string(p[:n])) //lo w
 fmt.Println(sr.Size())     //5
}

相關(guān)文章

  • 使用go net實現(xiàn)簡單的redis通信協(xié)議

    使用go net實現(xiàn)簡單的redis通信協(xié)議

    本文主要介紹了go net實現(xiàn)簡單的redis通信協(xié)議,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 詳解Go語言中ErrGroup的使用

    詳解Go語言中ErrGroup的使用

    本文主要為大家詳細(xì)介紹了Go語言中errGroup的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-07-07
  • Go語言的變量定義詳情

    Go語言的變量定義詳情

    這篇文章主要介紹了Go語言的變量定義詳情,go定義變量的方式和c,c++,java語法不一樣,var?變量名類型,var在前,變量名在中間,類型在后面,下文更多詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-03-03
  • go語言reflect.Type?和?reflect.Value?應(yīng)用示例詳解

    go語言reflect.Type?和?reflect.Value?應(yīng)用示例詳解

    這篇文章主要為大家介紹了go語言reflect.Type?和?reflect.Value?應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Go高級特性探究之對象比較詳解

    Go高級特性探究之對象比較詳解

    在go語言中,要比較兩個對象是否完全相同,我們可以使用三種方法,這篇文章主要為大家介紹了這三種方法的具體實現(xiàn),需要的可以參考一下
    2023-06-06
  • Go語言程序查看和診斷工具詳解

    Go語言程序查看和診斷工具詳解

    這篇文章主要為大家詳細(xì)介紹了Go語言程序查看和診斷工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Go語言接口的嵌套的具體使用

    Go語言接口的嵌套的具體使用

    在Go語言中,不僅結(jié)構(gòu)體與結(jié)構(gòu)體之間可以嵌套,接口與接口間也可以通過嵌套創(chuàng)造出新的接口,本文主要介紹了Go語言接口的嵌套的具體使用,感興趣的可以了解一下
    2023-04-04
  • Go語言排序與接口實例分析

    Go語言排序與接口實例分析

    這篇文章主要介紹了Go語言排序與接口,實例分析了排序與接口的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-02-02
  • 構(gòu)建Golang應(yīng)用最小Docker鏡像的實現(xiàn)

    構(gòu)建Golang應(yīng)用最小Docker鏡像的實現(xiàn)

    這篇文章主要介紹了構(gòu)建Golang應(yīng)用最小Docker鏡像的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Go語言中獲取IP地址的方法小結(jié)

    Go語言中獲取IP地址的方法小結(jié)

    這篇文章主要為大家詳細(xì)介紹了Go語言中獲取IP地址的常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12

最新評論