Golang bytes.Buffer.Read() function example
package bytes
Read reads the total length of the input bytes from the buffer or until the buffer is drained. The return value n (2nd parameter) is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless the input length is zero); otherwise it is nil.
Golang bytes.Buffer.Read() function usage example
package main
import (
"bytes"
"fmt"
)
func main() {
buff := bytes.NewBufferString("abcdef")
var threebytes [3]byte
n, err := buff.Read(threebytes[0:3])
fmt.Printf("%v %s\n", err, string(threebytes[:n]))
n, err = buff.Read(threebytes[:])
fmt.Printf("%v %s\n", err, string(threebytes[:n]))
n, err = buff.Read(threebytes[:])
fmt.Printf("%v %s\n", err, string(threebytes[:n]))
}
Output :
abc
def EOF
Reference :
Advertisement
Something interesting
Tutorials
+6.5k Golang : Convert an executable file into []byte example
+29.7k Golang : Record voice(audio) from microphone to .WAV file
+13k Swift : Convert (cast) Int to String ?
+7.5k Golang : How to stop user from directly running an executable file?
+12.5k Golang : Forwarding a local port to a remote server example
+7.6k Golang : Convert(cast) io.Reader type to string
+16.4k Golang : Send email and SMTP configuration example
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+8.4k Golang : How to check if input string is a word?
+5.1k Golang : Check if a word is countable or not
+9.6k Golang : How to generate Code 39 barcode?
+7.4k Golang : How to detect if a sentence ends with a punctuation?