Golang io.LimitReader() function example
package io
Golang io.LimitReader() function usage example
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
)
func main() {
reader := bytes.NewReader([]byte("abcdefghijklmnopqrstuvwxyz"))
data, err := ioutil.ReadAll(io.LimitReader(reader, 10)) // read 10 characters
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data))
data2, err := ioutil.ReadAll(io.LimitReader(reader, 20)) // read next 20 characters
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data2))
data3, err := ioutil.ReadAll(io.LimitReader(reader, 30)) // read next 30 characters
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data3)) // this will empty.
}
Output :
abcdefghij
klmnopqrstuvwxyz
References :
http://golang.org/pkg/io/#LimitReader
https://www.socketloop.com/references/golang-bytes-reader-len-function-example
See also : Golang bytes.Reader.Len() function example
Advertisement
Something interesting
Tutorials
+19.2k Golang : Delete item from slice based on index/key position
+4.7k MariaDB/MySQL : Form select statement or search query with Chinese characters
+17k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+10k Golang : Convert octal value to string to deal with leading zero problem
+10.1k Golang : Print how to use flag for your application example
+15.2k Golang : Get timezone offset from date or timestamp
+17k Golang : XML to JSON example
+19.8k Golang : Append content to a file
+27.6k PHP : Convert(cast) string to bigInt
+20.8k Golang : Underscore or snake_case to camel case example
+17.6k Convert JSON to CSV in Golang
+15.3k Golang : How to get Unix file descriptor for console and file