Golang之bytes.buffer
创建 Buffer缓冲器var b bytes.Buffer //直接定义一个 Buffer 变量,而不用初始化
b.Writer([]byte("Hello ")) // 可以直接使用
b1 := new(bytes.Buffer) //直接使用 new 初始化,可以直接使用
// 其它两种定义方式
func NewBuffer(buf []byte) *Buffer
func NewBufferString(s string) *Buffer
NewBuffer// NewBuffer creates and initializes a new Buffer using buf as its initial
// contents. It is intended to prepare a Buffer to read existing data. It
// can also be used to size the internal buffer for writing. To do that,
// buf should have the desired capacity but a length of zero.
//
// In most cases,new(Buffer) (or just declaring a Buffer variable) is
// sufficient to initialize a Buffer.
func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
func IntToBytes(n int) []byte {
x := int32(n)
//创建一个内容是[]byte的slice的缓冲器
//与bytes.NewBufferString("")等效
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer,binary.BigEndian,x)
return bytesBuffer.Bytes()
}
NewBufferString
// NewBufferString creates and initializes a new Buffer using string s as its
// initial contents. It is intended to prepare a buffer to read an existing
// string.
//
// In most cases,new(Buffer) (or just declaring a Buffer variable) is
// sufficient to initialize a Buffer.
func NewBufferString(s string) *Buffer {
return &Buffer{buf: []byte(s)}
}
func TestBufferString(){
buf1:=bytes.NewBufferString("swift")
buf2:=bytes.NewBuffer([]byte("swift"))
buf3:=bytes.NewBuffer([]byte{'s','w','i','f','t'})
fmt.Println("===========以下buf1,buf2,buf3等效=========")
fmt.Println("buf1:",buf1)
fmt.Println("buf2:",buf2)
fmt.Println("buf3:",buf3)
fmt.Println("===========以下创建空的缓冲器等效=========")
buf4:=bytes.NewBufferString("")
buf5:=bytes.NewBuffer([]byte{})
fmt.Println("buf4:",buf4)
fmt.Println("buf5:",buf5)
}
输出:
向 Buffer 中写入数据Write把字节切片 p 写入到buffer中去。 // Write appends the contents of p to the buffer,growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large,Write will panic with ErrTooLarge.
func (b *Buffer) Write(p []byte) (n int,err error) {
b.lastRead = opInvalid
m := b.grow(len(p))
return copy(b.buf[m:],p),nil
}
fmt.Println("===========以下通过Write把swift写入Learning缓冲器尾部=========")
newBytes := []byte("swift")
//创建一个内容Learning的缓冲器
buf := bytes.NewBuffer([]byte("Learning"))
//打印为Learning
fmt.Println(buf.String())
//将newBytes这个slice写到buf的尾部
buf.Write(newBytes)
fmt.Println(buf.String())
打印:
WriteString使用WriteString方法,将一个字符串放到缓冲器的尾部 // WriteString appends the contents of s to the buffer,growing the buffer as
// needed. The return value n is the length of s; err is always nil. If the
// buffer becomes too large,WriteString will panic with ErrTooLarge.
func (b *Buffer) WriteString(s string) (n int,err error) {
b.lastRead = opInvalid
m := b.grow(len(s))
return copy(b.buf[m:],s),nil
}
fmt.Println("===========以下通过WriteString把swift写入Learning缓冲器尾部=========")
newString := "swift"
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteString(newString)
fmt.Println(buf.String())
打印:
WriteByte将一个byte类型的数据放到缓冲器的尾部 // WriteByte appends the byte c to the buffer,growing the buffer as needed.
// The returned error is always nil,but is included to match bufio.Writer's
// WriteByte. If the buffer becomes too large,WriteByte will panic with
// ErrTooLarge.
func (b *Buffer) WriteByte(c byte) error {
b.lastRead = opInvalid
m := b.grow(1)
b.buf[m] = c
return nil
}
fmt.Println("===========以下通过WriteByte把!写入Learning缓冲器尾部=========")
var newByte byte = '!'
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteByte(newByte)
fmt.Println(buf.String())
打印:
WriteRune将一个rune类型的数据放到缓冲器的尾部 // WriteRune appends the UTF-8 encoding of Unicode code point r to the
// buffer,returning its length and an error,which is always nil but is
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
// if it becomes too large,WriteRune will panic with ErrTooLarge.
func (b *Buffer) WriteRune(r rune) (n int,err error) {
if r < utf8.RuneSelf {
b.WriteByte(byte(r))
return 1,nil
}
n = utf8.EncodeRune(b.runeBytes[0:],r)
b.Write(b.runeBytes[0:n])
return n,nil
}
fmt.Println("===========以下通过WriteRune把"好"写入Learning缓冲器尾部=========")
var newRune = '好'
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteRune(newRune)
fmt.Println(buf.String())
打印:
完整示例package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
//newBuffer 整形转换成字节
var n int = 10000
intToBytes := IntToBytes(n)
fmt.Println("==========int to bytes========")
fmt.Println(intToBytes)
//NewBufferString
TestBufferString()
//write
BufferWrite()
//WriteString
BufferWriteString()
//WriteByte
BufferWriteByte()
//WriteRune
BufferWriteRune()
}
func IntToBytes(n int) []byte {
x := int32(n)
//创建一个内容是[]byte的slice的缓冲器
//与bytes.NewBufferString("")等效
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer,x)
return bytesBuffer.Bytes()
}
func TestBufferString(){
buf1:=bytes.NewBufferString("swift")
buf2:=bytes.NewBuffer([]byte("swift"))
buf3:=bytes.NewBuffer([]byte{'s',buf5)
}
func BufferWrite(){
fmt.Println("===========以下通过Write把swift写入Learning缓冲器尾部=========")
newBytes := []byte("swift")
//创建一个内容Learning的缓冲器
buf := bytes.NewBuffer([]byte("Learning"))
//打印为Learning
fmt.Println(buf.String())
//将newBytes这个slice写到buf的尾部
buf.Write(newBytes)
fmt.Println(buf.String())
}
func BufferWriteString(){
fmt.Println("===========以下通过Write把swift写入Learning缓冲器尾部=========")
newString := "swift"
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteString(newString)
fmt.Println(buf.String())
}
func BufferWriteByte(){
fmt.Println("===========以下通过WriteByte把swift写入Learning缓冲器尾部=========")
var newByte byte = '!'
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteByte(newByte)
fmt.Println(buf.String())
}
func BufferWriteRune(){
fmt.Println("===========以下通过WriteRune把"好"写入Learning缓冲器尾部=========")
var newRune = '好'
//创建一个string内容Learning的缓冲器
buf := bytes.NewBufferString("Learning")
//打印为Learning
fmt.Println(buf.String())
//将newString这个string写到buf的尾部
buf.WriteRune(newRune)
fmt.Println(buf.String())
}
向 Buffer 中读取数据Read给Read方法一个容器p,读完后,p就满了,缓冲器相应的减少了,返回的n为成功读的数量 // Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return,err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int,err error) {}
func Read(){
bufs := bytes.NewBufferString("Learning swift.")
fmt.Println(bufs.String())
//声明一个空的slice,容量为8
l := make([]byte, 8)
//把bufs的内容读入到l内,因为l容量为8,所以只读了8个过来
bufs.Read(l)
fmt.Println("::bufs缓冲器内容::")
fmt.Println(bufs.String())
//空的l被写入了8个字符,所以为 Learning
fmt.Println("::l的slice内容::")
fmt.Println(string(l))
//把bufs的内容读入到l内,原来的l的内容被覆盖了
bufs.Read(l)
fmt.Println("::bufs缓冲器被第二次读取后剩余的内容::")
fmt.Println(bufs.String())
fmt.Println("::l的slice内容被覆盖,由于bufs只有7个了,因此最后一个g被留下来了::")
fmt.Println(string(l))
}
打印:
ReadByte返回缓冲器头部的第一个byte,缓冲器头部第一个byte被拿掉 // ReadByte reads and returns the next byte from the buffer.
// If no byte is available,it returns error io.EOF.
func (b *Buffer) ReadByte() (c byte,err error) {}
func ReadByte(){
bufs := bytes.NewBufferString("Learning swift.")
fmt.Println(bufs.String())
//读取第一个byte,赋值给b
b,_ := bufs.ReadByte()
fmt.Println(bufs.String())
fmt.Println(string(b))
}
打印:
ReadRuneReadRune和ReadByte很像 // ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available,the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding,it
// consumes one byte and returns U+FFFD,1.
func (b *Buffer) ReadRune() (r rune,size int,err error) {}
func ReadRune(){
bufs := bytes.NewBufferString("学swift.")
fmt.Println(bufs.String())
//读取第一个rune,赋值给r
r,z,_ := bufs.ReadRune()
//打印中文"学",缓冲器头部第一个被拿走
fmt.Println(bufs.String())
//打印"学","学"作为utf8储存占3个byte
fmt.Println("r=",string(r),",z=",z)
}
ReadBytesReadBytes需要一个byte作为分隔符,读的时候从缓冲器里找第一个出现的分隔符(delim),找到后,把从缓冲器头部开始到分隔符之间的所有byte进行返回,作为byte类型的slice,返回后,缓冲器也会空掉一部分 // ReadBytes reads until the first occurrence of delim in the input,
// returning a slice containing the data up to and including the delimiter.
// If ReadBytes encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func (b *Buffer) ReadBytes(delim byte) (line []byte,err error) {}
func ReadBytes(){
bufs := bytes.NewBufferString("现在开始 Learning swift.")
fmt.Println(bufs.String())
var delim byte = 'L'
line,_ := bufs.ReadBytes(delim)
fmt.Println(bufs.String())
fmt.Println(string(line))
}
打印:
ReadStringReadString需要一个byte作为分隔符,读的时候从缓冲器里找第一个出现的分隔符(delim),找到后,把从缓冲器头部开始到分隔符之间的所有byte进行返回,作为字符串,返回后,缓冲器也会空掉一部分 // ReadString reads until the first occurrence of delim in the input,
// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end
// in delim.
func (b *Buffer) ReadString(delim byte) (line string,err error) {}
ReadFrom从一个实现io.Reader接口的r,把r里的内容读到缓冲器里,n返回读的数量 // ReadFrom reads data from r until EOF and appends it to the buffer,growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large,ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64,err error) {}
func ReadFrom(){
//test.txt 内容是 "未来"
file,_ := os.Open("learngo/bytes/text.txt")
buf := bytes.NewBufferString("Learning swift.")
buf.ReadFrom(file) //将text.txt内容追加到缓冲器的尾部
fmt.Println(buf.String())
}
打印:
Reset将数据清空,没有数据可读 // Reset resets the buffer so it has no content.
// b.Reset() is the same as b.Truncate(0).
func (b *Buffer) Reset() { b.Truncate(0) }
func Reset(){
bufs := bytes.NewBufferString("现在开始 Learning swift.")
fmt.Println(bufs.String())
bufs.Reset()
fmt.Println("::已经清空了bufs的缓冲内容::")
fmt.Println(bufs.String())
}
打印:
string将未读取的数据返回成 string // String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer,it returns "<nil>".
func (b *Buffer) String() string {}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |