var buffer1 bytes.Buffer contents := "Simple byte buffer for marshaling data." fmt.Printf("Write contents %q ...\n", contents) buffer1.WriteString(contents) fmt.Printf("The length of buffer: %d\n", buffer1.Len()) fmt.Printf("The capacity of buffer: %d\n", buffer1.Cap()) fmt.Println()
Write contents "Simple byte buffer for marshaling data." ... //打印結果是這樣的 The length of buffer: 39 //這裏輸出了總長度 是可用長度 也就是未讀取的長度 The capacity of buffer: 64 //容量長度
1 // 示例2。 2 p1 := make([]byte, 7) 3 n, _ := buffer1.Read(p1) 4 fmt.Printf("%d bytes were read. (call Read)\n", n) 5 fmt.Printf("The length of buffer: %d\n", buffer1.Len()) 6 fmt.Printf("The capacity of buffer: %d\n", buffer1.Cap()) 7 fmt.Println(string(p1))
下面是輸出內容 咱們從緩衝區讀取了7個字節到p1字節切片裏 那麼buffer1的屬性off(計數器)會向後挪移7個字節 長度也會減去7個字節 咱們看下輸出結果spa
7 bytes were read. (call Read)
The length of buffer: 32
The capacity of buffer: 64code
再看下截取是示例:blog
buffer1.Truncate(6) //截取6個字節長度 從計數器爲起始位置