您好,登錄后才能下訂單哦!
bytes.Buffer怎么在golang中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
go是golang的簡稱,而golang可以做服務器端開發,且golang很適合做日志處理、數據打包、虛擬機處理、數據庫代理等工作。在網絡編程方面,它還廣泛應用于web應用、API應用等領域。
bytes.Buffer提供可擴容的字節緩沖區,實質是對切片的封裝;結構中包含一個64字節的小切片,避免小內存分配:
// A Buffer is a variable-sized buffer of bytes with Read and Write methods. // The zero value for Buffer is an empty buffer ready to use. type Buffer struct { buf []byte // contents are the bytes buf[off : len(buf)] off int // read at &buf[off], write at &buf[len(buf)]--->指示讀指針 bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation. lastRead readOp // last read operation, so that Unread* can work correctly. }
1) var buf bytes.Buffer ->定義一個空的字節緩沖區
2) func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} } -->將字節切片初始化為緩沖區
3) func NewBufferString(s string) *Buffer {return &Buffer{buf: []byte(s)}} -->將字符串初始化為緩沖區
// 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 }
// 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 //返回寫入的index m := b.grow(len(s)) return copy(b.buf[m:], s), nil }
// 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) { b.lastRead = opInvalid if b.off >= len(b.buf) { // Buffer is empty, reset to recover space. b.Truncate(0) if len(p) == 0 { return } return 0, io.EOF } n = copy(p, b.buf[b.off:]) b.off += n if n > 0 { b.lastRead = opRead } return }
// 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) { slice, err := b.readSlice(delim) return string(slice), err }
// Bytes returns a slice of length b.Len() holding the unread portion of the buffer. // The slice is valid for use only until the next buffer modification (that is, // only until the next call to a method like Read, Write, Reset, or Truncate). // The slice aliases the buffer content at least until the next buffer modification, // so immediate changes to the slice will affect the result of future reads. func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
// 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 { if b == nil { // Special case, useful in debugging. return "<nil>" } return string(b.buf[b.off:]) }
// Cap returns the capacity of the buffer's underlying byte slice, that is, the // total space allocated for the buffer's data. func (b *Buffer) Cap() int { return cap(b.buf) }
// Len returns the number of bytes of the unread portion of the buffer; // b.Len() == len(b.Bytes()). func (b *Buffer) Len() int { return len(b.buf) - b.off }
當向緩沖區寫入數據時,首先會檢查當前容量是否滿足需求,如果不滿足分三種情況處理:
1)當前內置緩沖區切片buf為空,且寫入數據量小于bootstrap的大小(64字節),則bootstrap作為buf
2)當前未讀數據長度+新寫入數據長度小于等于緩沖區容量的1/2,則挪動數據(將未讀的數據放到已讀數據位置)
3)以上條件不滿足,只能重新分配切片,容量設定為2*cap(b.buf) + n,即兩倍原來的緩沖區容量+寫入數據量大小
// grow grows the buffer to guarantee space for n more bytes. // It returns the index where bytes should be written. // If the buffer can't grow it will panic with ErrTooLarge. func (b *Buffer) grow(n int) int { m := b.Len() // If buffer is empty, reset to recover space. if m == 0 && b.off != 0 { b.Truncate(0) } //如果需要的容量大于現在的容量---> if len(b.buf)+n > cap(b.buf) { var buf []byte //現有的預備64byte可以滿足 if b.buf == nil && n <= len(b.bootstrap) { buf = b.bootstrap[0:] //實際需要的小于本身切片容量 } else if m+n <= cap(b.buf)/2 { // We can slide things down instead of allocating a new // slice. We only need m+n <= cap(b.buf) to slide, but // we instead let capacity get twice as large so we // don't spend all our time copying. copy(b.buf[:], b.buf[b.off:]) buf = b.buf[:m] } else { // not enough space anywhere //不夠,那就分配2倍加n的容量 buf = makeSlice(2*cap(b.buf) + n) copy(buf, b.buf[b.off:]) } b.buf = buf b.off = 0 } b.buf = b.buf[0 : b.off+m+n] return b.off + m }
bytes.Buffer提供了對切片的初步封裝,但也沒做太多的事;對于已讀的數據無法操作。
補充:Golang bytes.Buffer 用法精述
bytes.Buffer 是 Golang 標準庫中的緩沖區,具有讀寫方法和可變大小的字節存儲功能。緩沖區的零值是一個待使用的空緩沖區。
type Buffer struct { buf []byte // contents are the bytes buf[off : len(buf)] off int // read at &buf[off], write at &buf[len(buf)] lastRead readOp // last read operation, so that Unread* can work correctly. }
(1)從 bytes.Buffer 讀取數據后,被成功讀取的數據仍保留在原緩沖區,只是無法被使用,因為緩沖區的可見數據從偏移 off 開始,即buf[off : len(buf)]。
var b bytes.Buffer //直接定義一個Buffer變量,不用初始化,可以直接使用 b := new(bytes.Buffer) //使用New返回Buffer變量 b := bytes.NewBuffer(s []byte) //從一個[]byte切片,構造一個Buffer b := bytes.NewBufferString(s string) //從一個string變量,構造一個Buffer
b.Write(d []byte) (n int, err error) //將切片d寫入Buffer尾部 b.WriteString(s string) (n int, err error) //將字符串s寫入Buffer尾部 b.WriteByte(c byte) error //將字符c寫入Buffer尾部 b.WriteRune(r rune) (n int, err error) //將一個rune類型的數據放到緩沖區的尾部 b.ReadFrom(r io.Reader) (n int64, err error) //從實現了io.Reader接口的可讀取對象寫入Buffer尾部
//讀取 n 個字節數據并返回,如果 buffer 不足 n 字節,則讀取全部 b.Next(n int) []byte //一次讀取 len(p) 個 byte 到 p 中,每次讀取新的內容將覆蓋p中原來的內容。成功返回實際讀取的字節數,off 向后偏移 n,buffer 沒有數據返回錯誤 io.EOF b.Read(p []byte) (n int, err error) //讀取第一個byte并返回,off 向后偏移 n b.ReadByte() (byte, error) //讀取第一個 UTF8 編碼的字符并返回該字符和該字符的字節數,b的第1個rune被拿掉。如果buffer為空,返回錯誤 io.EOF,如果不是UTF8編碼的字符,則消費一個字節,返回 (U+FFFD,1,nil) b.ReadRune() (r rune, size int, err error) //讀取緩沖區第一個分隔符前面的內容以及分隔符并返回,緩沖區會清空讀取的內容。如果沒有發現分隔符,則返回讀取的內容并返回錯誤io.EOF b.ReadBytes(delimiter byte) (line []byte, err error) //讀取緩沖區第一個分隔符前面的內容以及分隔符并作為字符串返回,緩沖區會清空讀取的內容。如果沒有發現分隔符,則返回讀取的內容并返回錯誤 io.EOF b.ReadString(delimiter byte) (line string, err error) //將 Buffer 中的內容輸出到實現了 io.Writer 接口的可寫入對象中,成功返回寫入的字節數,失敗返回錯誤 b.WriteTo(w io.Writer) (n int64, err error)
b.Bytes() []byte //返回字節切片 b.Cap() int //返回 buffer 內部字節切片的容量 b.Grow(n int) //為 buffer 內部字節切片的容量增加 n 字節 b.Len() int //返回緩沖區數據長度,等于 len(b.Bytes()) b.Reset() //清空數據 b.String() string //字符串化 b.Truncate(n int) //丟棄緩沖區中除前n個未讀字節以外的所有字節。如果 n 為負數或大于緩沖區長度,則引發 panic b.UnreadByte() error //將最后一次讀取操作中被成功讀取的字節設為未被讀取的狀態,即將已讀取的偏移 off 減 1 b.UnreadRune() error //將最后一次 ReadRune() 讀取操作返回的 UTF8 字符 rune設為未被讀取的狀態,即將已讀取的偏移 off 減去 字符 rune 的字節數
test.txt 的內容為:
My name is dablelv
具體實現:
package main import ( "os" "fmt" "bytes" ) func main() { file, _ := os.Open("./test.txt") buf := bytes.NewBufferString("Hello world ") buf.ReadFrom(file) //將text.txt內容追加到緩沖器的尾部 fmt.Println(buf.String()) }
編譯運行輸出:
Hello world My name is dablelv
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。