Golang bufio.Available() function example

package bufio

Available returns how many bytes are unused in the buffer.

Golang bufio.Available() 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()
 fmt.Printf("Bytes available : %d\n", screenWriter.Available())
 }

Output :

Bytes available : 8166

Bytes available : 8192

Advertisement