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
+6.8k Golang : Join lines with certain suffix symbol example
+55.3k Golang : Unmarshal JSON from http response
+6.1k Golang : Debug with Godebug
+14k Golang : concatenate(combine) strings
+9.4k Golang : Timeout example
+5.7k Linux/Unix/PHP : Restart PHP-FPM
+11.6k Golang : Simple file scaning and remove virus example
+14k Golang : Compress and decompress file with compress/flate example
+12.3k Golang : Validate email address
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode