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()
Contents the Write " . Marshaling for the Simple Data Buffer byte " ... // print result is such of The length of Buffer: 39 // output here is the total length of the length of the usable length is not read of The Capacity of Buffer: 64 // length capacity
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))
Here is the output of what we read 7 bytes from the buffer to p1 byte slice in so buffer1 properties off (counter) will back the diversion of minus 7 bytes 7 bytes length will we see next Output result
7 bytes were read. (call Read)
The length of buffer: 32
The capacity of buffer: 64
Look at sample is taken:
buffer1.Truncate ( . 6 ) // taken six byte length from the start position of the counter