自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang 中的 Io 包詳解(一):基礎(chǔ)接口

開發(fā) 前端
Read 方法讀取長度為 len(p) 字節(jié)的數(shù)據(jù)到 p 中,返回兩個參數(shù),讀取數(shù)據(jù)的字節(jié)數(shù)(0 <= n <= len(p))和 error 信息。當(dāng) Read 在讀取 n>0 個字節(jié)后遇到錯誤或 EOF 時,返回讀取的字節(jié)數(shù)和一個可能為非空的 error,下次接著讀取時,會返回 0 和 非空 error。

Golang 中的 io 包提供了許多用于處理 I/O(輸入輸出) 操作的接口和函數(shù),在許多標(biāo)準(zhǔn)庫中都可以看到這些接口和函數(shù)的應(yīng)用。本文首先介紹一下 io 包的幾個基礎(chǔ)接口。

io.Reader

io.Reader 表示任何可以讀取數(shù)據(jù)的對象,定義了基本的 Read 方法。

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

Read 方法讀取長度為 len(p) 字節(jié)的數(shù)據(jù)到 p 中,返回兩個參數(shù),讀取數(shù)據(jù)的字節(jié)數(shù)(0 <= n <= len(p))和 error 信息。當(dāng) Read 在讀取 n>0 個字節(jié)后遇到錯誤或 EOF 時,返回讀取的字節(jié)數(shù)和一個可能為非空的 error,下次接著讀取時,會返回 0 和 非空 error??磦€示例:

package main

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

func main() {
	f, err := os.Open("test.txt")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	buf := make([]byte, 4) // 實例化一個長度為4的[]byte
	i := 0
	for {
		i++
		n, err := f.Read(buf) 
		fmt.Printf("第 %d 次讀取, ", i)
		fmt.Printf("返回行數(shù):%d,error 信息:%v ", n, err)

		if n == 0 || err == io.EOF {
			fmt.Println("文件以讀取完畢")
			break
		}
		fmt.Printf("讀取內(nèi)容:%s \n", string(buf[:n]))
	}
}

將 test.txt 文件內(nèi)容設(shè)置為 3 個字節(jié) lud,運行看下效果:

第 1 次讀取, 返回行數(shù):3,error 信息:<nil> 讀取內(nèi)容:lud 
第 2 次讀取, 返回行數(shù):0,error 信息:EOF 文件以讀取完畢

將 test.txt 文件內(nèi)容設(shè)置為 4 個字節(jié) ludu,運行看下效果:

第 1 次讀取, 返回行數(shù):4,error 信息:<nil> 讀取內(nèi)容:ludu 
第 2 次讀取, 返回行數(shù):0,error 信息:EOF 文件以讀取完畢

將 test.txt 文件內(nèi)容設(shè)置為 5 個字節(jié) luduo,運行看下效果:

第 1 次讀取, 返回行數(shù):4,error 信息:<nil> 讀取內(nèi)容:ludu 
第 2 次讀取, 返回行數(shù):1,error 信息:<nil> 讀取內(nèi)容:o 
第 3 次讀取, 返回行數(shù):0,error 信息:EOF 文件以讀取完畢

io.Writer

Writer 接口表示一個可以寫入數(shù)據(jù)的對象,定義了基本的 Write 方法。

type Writer interface {
	Write(p []byte) (n int, err error)
}

Write方法寫入長度為 len(p) 字節(jié)的數(shù)據(jù)到基本數(shù)據(jù)流中,返回實際寫入的字節(jié)數(shù) n 和 error 信息。若 Write 返回的 n < len(p),同時會返回一個非 nil 的 error。簡單示例如下:

package main

import (
	"fmt"
	"os"
)

func main() {
	file, err := os.Create("file.txt")
	if err != nil {
		panic(err)
	}

	defer file.Close()

	data := []byte("luduoxin's blog")

	n, err := file.Write(data)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%d bytes written", n)
}

io.Closer

Closer 接口表示可以關(guān)閉的對象,定義了一個基本的 Close 方法,通常在完成讀寫后關(guān)閉IO流以釋放資源。

type Closer interface {
	Close() error
}

Close 方法用于釋放資源,返回可能出現(xiàn)的 error,簡單示例如下:

import (
    "os"
)

func main() {
    file, err := os.Create("file.txt")
    if err != nil {
        panic(err)
    }

    // 在文件使用結(jié)束后,需要調(diào)用 Close 方法釋放資源。
    defer file.Close()

    // 文件讀寫操作...
}

io.Seeker

Seeker 接口表示可以隨機(jī)讀寫的對象,定義了基本的 Seek 方法。Seek方法定位到給定偏移量位置,返回新的 offset 和 error 信息。

type Seeker interface {
	Seek(offset int64, whence int) (int64, error)
}

Seek 方法將當(dāng)前讀或?qū)懳恢迷O(shè)置為距離偏移量 offset 個字節(jié)之后的位置。參數(shù) whence 可以是 0、1 或 2:

  • 參數(shù) whence 為 0 時,表示相對于文件起始位置,offset 必須為非負(fù)數(shù)。
  • 參數(shù) whence 為 1 時,表示相對于當(dāng)前位置,offset 可以為負(fù)數(shù)。
  • 參數(shù) whence 為 2 時,表示相對于文件結(jié)尾,offset 可以為負(fù)數(shù)。

Seek 方法將返回新的偏移量和可能出現(xiàn)的錯誤。示例如下:

package main

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

func main() {
	file, err := os.Create("file.txt")
	if err != nil {
		panic(err)
	}

	defer file.Close()

	// 寫入數(shù)據(jù)到文件中...
	file.Write([]byte("hello world"))

	// 將文件指針移動到文件開頭
	file.Seek(0, io.SeekStart)

	// 從文件中讀取數(shù)據(jù)到切片中
	buf := make([]byte, 5)
	file.Read(buf)
	fmt.Println(string(buf)) // 輸出 hello

	// 將文件指針移動到文件末尾
	file.Seek(0, io.SeekEnd)

	// 在文件末尾進(jìn)行寫入操作
	file.Write([]byte("golang"))
}

通過調(diào)用 Seek 方法將文件指針移動到文件開頭,并從文件中讀取了數(shù)據(jù),接下來將文件指針移動到文件末尾,并在文件末尾寫入了新的數(shù)據(jù)。

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

2023-08-03 08:48:07

Golang接口

2023-08-08 14:51:29

2023-08-31 09:28:12

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

2023-08-07 09:18:32

Golang偏移量接口

2023-08-28 17:16:51

Golangio 包

2023-05-12 09:40:53

ContextGolang

2023-03-30 07:52:03

Golang接口

2023-11-27 15:02:37

BytesGolang

2023-10-18 08:22:38

BufioGolang

2023-11-03 08:53:15

StrconvGolang

2023-09-05 08:22:44

Golangstrings 包

2023-10-07 09:08:32

Golangbufio

2023-10-10 08:57:44

Golangbufio

2024-01-18 09:07:04

Errors函數(shù)Golang

2023-09-06 09:10:04

Golang字符串

2023-11-07 09:02:07

Golangbytes

2023-09-07 07:35:54

GolangBufio

2023-09-04 08:17:37

Golangstrings 包

2023-11-13 21:55:12

Go編程

2023-05-15 08:50:58

ContextGolang
點贊
收藏

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