golang講解(go語言)標準庫分析之io完結篇

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

今天我們繼續講golang標準庫的io庫,我們今天就把io庫就講完了,所以就不多說了,讓給我們的講解和代碼

[1]type Reader
[php]
type Reader interface {
Read(p []byte) (n int, err error)
}
[/php]
(1)func LimitReader(r Reader, n int64) Reader,我們之前就說了Reader這個結構,其實這就是對Reader的一次封裝,限定了它讀取位元組數,其實他實現的就是io.LimitedReader{}這個結構
[php]
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
}

}
[/php]

(2)func MultiReader(readers ...Reader) Reader這個函數一看就知道是封裝了多個readers,跟上邊的方法差不多,只是封裝了多個而已,當然還去除了讀取的限制,我們代碼給大家測試一下
[php]
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
}
[/php]

(3)既然上邊介紹讀了,我這介紹個寫吧type Write`func MultiWriter(writers ...Writer) Writer一樣的作用只不過是這次換成寫了
[php]
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
}
[/php]

(4)func TeeReader(r Reader, w Writer) Reader這個方法有意思是從r中讀取資料然後寫入到w中,這個沒有內部緩衝區,看下代碼
[php]
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
}
[/php]

[2]type SectionReader{}
[php]
type SectionReader struct {
// contains filtered or unexported fields
}
[/php]
(1)func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader,你一看就知道了,其實就是通過這個方法擷取到io.SectionReader,第一個參數讀取器,第二個參數位移量,第三個參數是讀取多少

[php]
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
}
[/php]

(2)func (s *SectionReader) Read(p []byte) (n int, err error)熟悉的read()其實就是讀取資料用的,大家看函數就可以理解了,因為咱們經常遇到這個上兩個都寫這個了~~

[php]
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
}
[/php]

(3)func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)額這個跟之前的ReadAt是一樣的,只不過只有一個位移量,少了截取數,但是你要知道SectionReader做的是什麼就把資料截取了,所以就不需要截取數了

[php]
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
}

}
[/php]
(4)func (s *SectionReader) Seek(offset int64, whence int) (int64, error)這個是設定檔案指標的便宜量的,之前我們的os裡邊也是有個seek的,對SectionReader的讀取起始點、當前讀取點、結束點進行位移,offset 位移量,whence 設定選項 0:讀取起始點,1:當前讀取點,2:結束點(不好用),其他:將拋出Seek: invalid whence異常

[php]
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) //相當於起始的地址位移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w 是不是達到了前邊的ReadAt()
}
[/php]

(5)func (s *SectionReader) Size() int64返回的是可以讀取的位元組數,這個不受位移指標的影響,也不受當前讀取的影響,我們具體看下代碼

[php]
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) //相當於起始的地址位移1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w
fmt.Println(sr.Size()) //5
}
[/php]

今天我們就講完了io的標準庫了,明天講哪個呢?還是關註明天吧哈!每天只講一點golang的標準庫,方便大家學習和使用,更多的時候去理解標準庫,大家多動手,如果你喜歡請繼續關注我們!
代碼和檔案託管到github上,github地址https://github.com/widuu/gopkg

Golang標準庫

未經允許,不得轉載本站任何文章:微度網路 » golang講解(go語言)標準庫分析之io完結篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.