Golang bufio.NewWriterSize() function example
package bufio
NewWriterSize returns a new Writer whose buffer has at least the specified size. If the argument io.Writer is already a Writer with large enough size, it returns the underlying Writer.
Golang bufio.NewWriterSize() function usage example
package main
import (
"bufio"
"fmt"
"bytes"
)
func main() {
screenBuffer := bytes.NewBuffer(nil)
screenWriter := bufio.NewWriterSize(screenBuffer, 8192) // our buffer has size of 8192 = 8KB
fmt.Fprint(screenWriter, "Hello, ")
fmt.Fprint(screenWriter, "world! 你好世界")
// print empty size buffer
fmt.Printf("Buffer size is %d bytes for string [%s] \n", len(screenBuffer.Bytes()), string(screenBuffer.Bytes()))
// flush to buffer
screenWriter.Flush()
fmt.Printf("Buffer size is %d bytes for string [%s] \n", len(screenBuffer.Bytes()), string(screenBuffer.Bytes()))
}
Output :
Buffer size is 0 bytes for string []
Buffer size is 26 bytes for string [Hello, world! 你好世界]
Advertisement
Something interesting
Tutorials
+10k Golang : Convert octal value to string to deal with leading zero problem
+10.1k Golang : Test a slice of integers for odd and even numbers
+15.3k nginx: [emerg] unknown directive "ssl"
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+4.9k Nginx and PageSpeed build from source CentOS example
+9.7k Golang : Populate slice with sequential integers example
+7.8k Golang : Reverse a string with unicode
+6.1k Golang : Scan forex opportunities by Bollinger bands
+5k Linux : How to set root password in Linux Mint
+31.6k Golang : Get local IP and MAC address
+6.6k Golang : How to determine if request or crawl is from Google robots