Golang io.SectionReader.Size function example
package io
Golang io.SectionReader.Size function usage example
package main
import (
"bytes"
"fmt"
"io"
)
func main() {
reader := bytes.NewReader([]byte("abcdefghijklmnopqrstuvwxyz"))
// read from position 2 to 8
// remember...counting starts from zero
sectionReader := io.NewSectionReader(reader, 0, 8)
fmt.Printf("Read up to %d characters(bytes)\n", sectionReader.Size())
buff := make([]byte, 7)
// read section into buff
n, err := sectionReader.Read(buff)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Read %d characters(bytes) of %s \n", n, string(buff[:]))
}
Output :
Read up to 8 characters(bytes)
Read 7 characters(bytes) of abcdefg
Reference :
Advertisement
Something interesting
Tutorials
+16.3k Golang :Trim white spaces from a string
+20.2k Golang : Count number of digits from given integer value
+5.7k Golang : Struct field tags and what is their purpose?
+6.9k Mac/Linux/Windows : Get CPU information from command line
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+7.3k Golang : Not able to grep log.Println() output
+15.3k Golang : How to get Unix file descriptor for console and file
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+28.2k Golang : Connect to database (MySQL/MariaDB) server
+9k Golang : automatically figure out array length(size) with three dots
+6.9k Golang : Normalize email to prevent multiple signups example
+4.6k JavaScript : Rounding number to decimal formats to display currency