关于golang:go的-bytesbuffer-缓冲器

0次阅读

共计 3823 个字符,预计需要花费 10 分钟才能阅读完成。

一、创立缓冲期

bytes.buffer 是一个缓冲 byte 类型的缓冲器

1、应用 bytes.NewBuffer 创立:参数是 []byte 的话,缓冲器里就是这个 slice 的内容;如果参数是 nil 的话,就是创立一个空的缓冲器。

2、bytes.NewBufferString 创立

3、bytes.Buffer{} 

func main(){buf1 := bytes.NewBufferString("hello")
   buf2 := bytes.NewBuffer([]byte("hello"))
   buf3 := bytes.NewBuffer([]byte{'h','e','l','l','o'})
   以上三者等效, 输入 //hello
   buf4 := bytes.NewBufferString("")
   buf5 := bytes.NewBuffer([]byte{})
   以上两者等效, 输入 //""
   fmt.Println(buf1.String(),buf2.String(),buf3.String(),buf4,buf5,1)
}

二、写入到缓冲器

如果 buffer 在 new 的时候是空的,能够用 Write 在尾部写入

1、Write 办法,将一个 byte 类型的 slice 放到缓冲器的尾部

//func (b *Buffer) Write(p []byte) (n int,err error)

func main(){s := []byte("world")
   buf := bytes.NewBufferString("hello") 
   fmt.Println(buf.String())    //hello
   buf.Write(s)                 // 将 s 这个 slice 增加到 buf 的尾部
   fmt.Println(buf.String())   //hello world
}

2、WriteString 办法,把一个字符串放到缓冲器的尾部

 //func (b *Buffer) WriteString(s string)(n int,err error)

func main(){
   s := "world"
   buf := bytes.NewBufferString("hello")
   fmt.Println(buf.String())    //hello
   buf.WriteString(s)           // 将 string 写入到 buf 的尾部
   fmt.Println(buf.String())    //hello world
}

3、WriteByte 办法,将一个 byte 类型的数据放到缓冲器的尾部

 //func (b *Buffer) WriteByte(c byte) error


func main(){
   var s byte = '?'
   buf := bytes.NewBufferString("hello") 
   fmt.Println(buf.String()) // 把 buf 的内容转换为 string,hello
   buf.WriteByte(s)         // 将 s 写到 buf 的尾部
   fmt.Println(buf.String()) //hello?}

4、WriteRune 办法,将一个 rune 类型的数据放到缓冲器的尾部

// func (b *Buffer) WriteRune(r Rune) (n int,err error)

func main(){
   var s rune = '好'
   buf := bytes.NewBufferString("hello")
   fmt.Println(buf.String()) //hello
   buf.WriteRune(s)   
   fmt.Println(buf.String()) //hello 好
}

三、从缓冲器写出

WriteTo 办法,将一个缓冲器的数据写到 w 里,w 是实现 io.Writer 的,比方 os.File

func main() {file, _ := os.Create("text.txt")
    buf := bytes.NewBufferString("hello world")
    buf.WriteTo(file)
    // 或者应用写入,fmt.Fprintf(file,buf.String())
}

四、读出缓冲器

1、Read 办法,给 Read 办法一个容器,读完后 p 就满了,缓冲器相应的缩小。

// func (b *Buffer) Read(p []byte)(n int,err error)

func main(){s1 := []byte("hello")
   buff := bytes.NewBuffer(s1)
   s2 := []byte("world")
   buff.Write(s2)
   fmt.Println(buff.String()) //hello world
   
   s3 := make([]byte,3)
   buff.Read(s3)     // 把 buff 的内容读入到 s3,s3 的容量为 3,读了 3 个过去
   fmt.Println(buff.String()) //lo world
   fmt.Println(string(s3))   //hel
   buff.Read(s3) // 持续读入 3 个,原来的被笼罩
   
   fmt.Println(buff.String())     //world
   fmt.Println(string(s3))    //"lo"
}

2、ReadByte 办法,返回缓冲器头部的第一个 byte,缓冲器头部第一个 byte 取出

//func (b *Buffer) ReadByte() (c byte,err error)

func main(){buf := bytes.NewBufferString("hello")
   fmt.Println(buf.String())
   b,_ := buf.ReadByte()   // 取出第一个 byte,赋值给 b
   fmt.Println(buf.String()) //ello
   fmt.Println(string(b))   //h
}

3、ReadRune 办法,返回缓冲器头部的第一个 rune

// func (b *Buffer) ReadRune() (r rune,size int,err error)
func main(){buf := bytes.NewBufferString("你好 smith")
   fmt.Println(buf.String())
   b,n,_ := buf.ReadRune()  // 取出第一个 rune
   fmt.Println(buf.String()) // 好 smith
   fmt.Println(string(b))   // 你
   fmt.Println(n)   //3," 你“作为 utf8 存储占 3 个 byte
   b,n,_ = buf.ReadRune()  // 再取出一个 rune
   fmt.Println(buf.String()) //smith
   fmt.Println(string(b))  // 好
   fmt.Println(n)   //3
}

4、ReadBytes 办法,须要一个 byte 作为分隔符,读的时候从缓冲器里找出第一个呈现的分隔符,缓冲器头部开始到分隔符之间的 byte 返回。

//func (b *Buffer) ReadBytes(delim byte) (line []byte,err error)

func main(){
   var d byte = 'e'  // 分隔符
   buf := bytes.NewBufferString("你好 esmieth")
   fmt.Println(buf.String()) // 你好 esmieth
   b,_ := buf.ReadBytes(d)  // 读到分隔符,并返回给 b
   fmt.Println(buf.String())  //smieth
   fmt.Println(string(b)) // 你好 e
}

5、ReadString 办法,和 ReadBytes 办法一样

//func (b *Buffer) ReadString(delim byte) (line string,err error)

func main(){
   var d byte = 'e'
   buf := bytes.NewBufferString("你好 esmieth")
   fmt.Println(buf.String())  // 你好 esmieth
   b,_ := buf.ReadString(d)   // 读取到分隔符,并返回给 b
   fmt.Println(buf.String()) //smieth
   fmt.Println(string(b)) // 你好 e
}

五、读入缓冲器

ReadFrom 办法,从一个实现 io.Reader 接口的 r,把 r 的内容读到缓冲器里,n 返回读的数量

//func (b *Buffer) ReadFrom(r io.Reader) (n int64,err error)

func main(){file, _ := os.Open("text.txt")
   buf := bytes.NewBufferString("bob")
   buf.ReadFrom(file)
   fmt.Println(buf.String()) //bob hello world
}

六、从缓冲器取出

Next 办法,返回前 n 个 byte(slice),原缓冲器变小

//func (b *Buffer) Next(n int) []byte
func main(){buf := bytes.NewBufferString("hello world")
   fmt.Println(buf.String())
   b := buf.Next(2)  // 取前 2 个
   fmt.Println(buf.String()) //llo world
   fmt.Println(string(b)) //he
}

残缺代码案例参考上面链接

缓冲期

原文参考

https://my.oschina.net/u/943306/blog/127981

正文完
 0