成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Golang 中的 IO 包詳解:結(jié)構(gòu)體類型介紹

開發(fā) 后端
實(shí)現(xiàn)了Io.Reader、Io.ReaderAt 和 Io.Seeker 接口的類型,用于在一個(gè) Reader 中只讀取某部分的數(shù)據(jù)。在使用 Io.SectionReader 時(shí),通常是將其作為參數(shù)傳遞給其他需要 ReaderAt 或 Seeker 接口的函數(shù),并在該函數(shù)中使用 ReadAt 或 Seek 方法來訪問數(shù)據(jù)。

io.LimitedReader

// A LimitedReader reads from R but limits the amount of
// data returned to just N bytes. Each call to Read
// updates N to reflect the new amount remaining.
// Read returns EOF when N <= 0 or when the underlying R returns EOF.
type LimitedReader struct {
	R Reader // underlying reader
	N int64  // max bytes remaining
}

實(shí)現(xiàn)了 io.Reader 接口,并且進(jìn)行了功能擴(kuò)展。R 表示 io.Reader 對(duì)象,N 表示最多允許讀取的字節(jié)數(shù)。簡(jiǎn)單示例如下所示:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	reader := io.LimitReader(bytes.NewReader(data), 5)
	buf := make([]byte, 10)
	n, err := reader.Read(buf)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

當(dāng)讀取的字節(jié)數(shù)超過限制時(shí),LimitedReader 會(huì)自動(dòng)終止讀取并返回一個(gè) io.EOF 錯(cuò)誤,表示已經(jīng)達(dá)到了總字節(jié)數(shù)的限制。

io.SectionReader

// SectionReader implements Read, Seek, and ReadAt on a section
// of an underlying ReaderAt.
type SectionReader struct {
	r     ReaderAt
	base  int64
	off   int64
	limit int64
}

實(shí)現(xiàn)了 io.Reader、io.ReaderAt 和 io.Seeker 接口的類型,用于在一個(gè) Reader 中只讀取某部分的數(shù)據(jù)。在使用 io.SectionReader 時(shí),通常是將其作為參數(shù)傳遞給其他需要 ReaderAt 或 Seeker 接口的函數(shù),并在該函數(shù)中使用 ReadAt 或 Seek 方法來訪問數(shù)據(jù)。簡(jiǎn)單示例如下:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	reader := bytes.NewReader(data)
	sectionReader := io.NewSectionReader(reader, 0, 6)
	buf := make([]byte, 5)
	n, err := sectionReader.ReadAt(buf, 0)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

io.teeReader

type teeReader struct {
	r Reader
	w Writer
}

實(shí)現(xiàn)了 io.Reader 和 io.Writer 接口的類型,可以將輸入流的內(nèi)容復(fù)制到一個(gè)指定的輸出流中。簡(jiǎn)單示例如下:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	buf1 := bytes.NewBuffer(nil)
	buf2 := bytes.NewBuffer(nil)
	reader := bytes.NewReader(data)
	tee := io.TeeReader(reader, io.MultiWriter(buf1, buf2))
	buf := make([]byte, 10)
	n, err := tee.Read(buf)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
		fmt.Printf("%s\n", buf1.Bytes())
		fmt.Printf("%s\n", buf2.Bytes())
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

io.PipeReader

// A PipeReader is the read half of a pipe.
type PipeReader struct {
	p *pipe
}

io.PipeReader 用于從 io.Pipe 中讀取數(shù)據(jù)的類型。io.Pipe 實(shí)際上是一個(gè)管道,可以用于在同一個(gè)進(jìn)程中的不同 goroutine 之間傳輸數(shù)據(jù)。PipeReader 實(shí)際上是通過 io.Pipe 返回的讀取端實(shí)例。使用起來非常簡(jiǎn)單,可以通過 io.Pipe 函數(shù)創(chuàng)建一個(gè) Pipe 實(shí)例,io.Pipe 函數(shù)返回的是兩個(gè)值分別是 io.PipeReader 和 io.PipeWriter 類型的指針,前者用于從管道中讀取數(shù)據(jù),后者用于向管道中寫入數(shù)據(jù)。簡(jiǎn)單示例如下:

package main

import (
	"bufio"
	"fmt"
	"io"
)

func main() {
	pr, pw := io.Pipe()
	go func() {
		pw.Write([]byte("hello, world!"))
		pw.Close()
	}()
	br := bufio.NewReader(pr)
	line, isPrefix, err := br.ReadLine()
	fmt.Println(line, isPrefix, err)
}

io.PipeWriter

// A PipeWriter is the write half of a pipe.
type PipeWriter struct {
	p *pipe
}

io.PipeWriter 是用于向 io.Pipe 中寫入數(shù)據(jù)的類型。io.Pipe 實(shí)際上是一個(gè)管道,可以用于在同一個(gè)進(jìn)程中的不同 goroutine 之間傳輸數(shù)據(jù)。PipeWriter 實(shí)際上是通過 io.Pipe 返回的寫入端實(shí)例。io.Pipe 使用起來非常簡(jiǎn)單,可以通過 io.Pipe 函數(shù)創(chuàng)建一個(gè) Pipe 實(shí)例,io.Pipe 函數(shù)返回的是兩個(gè)值,分別是 io.PipeReader 和 io.PipeWriter 類型的指針,前者用于從管道中讀取數(shù)據(jù),后者用于向管道中寫入數(shù)據(jù)。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2023-08-03 08:48:07

Golang接口

2023-06-09 08:16:09

GolangStruct Tag

2023-08-02 09:07:27

Golangio 包

2023-08-31 09:28:12

Golang可導(dǎo)出函數(shù)

2023-08-08 14:51:29

2022-01-09 23:04:19

語言打印結(jié)構(gòu)體

2023-08-07 09:18:32

Golang偏移量接口

2023-11-27 15:02:37

BytesGolang

2023-10-31 09:10:39

2023-10-18 08:22:38

BufioGolang

2023-05-12 09:40:53

ContextGolang

2023-11-07 09:02:07

Golangbytes

2023-09-06 09:10:04

Golang字符串

2023-09-07 07:35:54

GolangBufio

2023-09-04 08:17:37

Golangstrings 包

2023-09-05 08:22:44

Golangstrings 包

2024-01-18 09:07:04

Errors函數(shù)Golang

2023-10-10 08:57:44

Golangbufio

2023-11-03 08:53:15

StrconvGolang

2023-10-07 09:08:32

Golangbufio
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 色视频网站 | 九九亚洲 | 天天草天天干 | 一级a性色生活片久久毛片 午夜精品在线观看 | 亚洲va国产日韩欧美精品色婷婷 | 九九天堂网 | 婷婷久久一区 | 亚洲欧洲一区 | 色婷婷av久久久久久久 | 91精品国产综合久久久亚洲 | 亚洲三区在线观看 | 午夜久久久| 亚洲国产激情 | 日日夜夜精品视频 | av香蕉 | 国产精品免费一区二区三区 | 国产农村妇女毛片精品久久麻豆 | 精品视频一区二区三区在线观看 | 日韩av大片免费看 | 99免费在线观看视频 | 国产精品久久久亚洲 | 成人免费观看男女羞羞视频 | 日韩不卡一区二区三区 | 久久精品亚洲成在人线av网址 | 国产95在线 | 午夜羞羞 | 色视频网站 | 亚洲三级在线观看 | 一本色道久久综合亚洲精品高清 | 国产色 | 密色视频| 性一交一乱一透一a级 | 成人久久18免费网站麻豆 | 毛片一级黄色 | 婷婷99 | chinese中国真实乱对白 | 亚洲www啪成人一区二区 | 国产精品美女久久久久久免费 | 婷婷综合五月天 | 黄色大片免费观看 | 91精品国产欧美一区二区成人 |