Golang bufio.Flush() function example
package bufio
Flush writes any buffered data to the underlying io.Writer.
Golang bufio.Flush() function usage example
package main
import (
"bufio"
"fmt"
"bytes"
)
func main() {
screenBuffer := bytes.NewBuffer(nil)
screenWriter := bufio.NewWriterSize(screenBuffer, 8192) // 8192 = 8KB
// these two lines will take up 26 bytes from screenWriter
fmt.Fprint(screenWriter, "Hello, ")
fmt.Fprint(screenWriter, "world! 你好世界")
// print the initial size buffer ...
fmt.Printf("Bytes available : %d\n", screenWriter.Available())
// flush the buffer and free up the 26 bytes
screenWriter.Flush() // <--------------- flush is important! just like flushing your toilet
fmt.Printf("Bytes available : %d\n", screenWriter.Available())
}
Advertisement
Something interesting
Tutorials
+20.2k Golang : Determine if directory is empty with os.File.Readdir() function
+11.9k Golang : Convert decimal number(integer) to IPv4 address
+5.9k Golang : Detect variable or constant type
+37.5k Golang : Converting a negative number to positive number
+6.3k Golang : Extract sub-strings
+10.2k Golang : Random Rune generator
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+9k Golang : Get SPF and DMARC from email headers to fight spam
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+5.7k Get website traffic ranking with Similar Web or Alexa
+14k Golang : Human readable time elapsed format such as 5 days ago
+12.3k Golang : Get month name from date example