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
+21.2k Golang : How to force compile or remove object files first before rebuild?
+11.1k Golang : Read until certain character to break for loop
+40.5k Golang : Convert to io.ReadSeeker type
+10k Golang : Read file and convert content to string
+8.8k Golang : Heap sort example
+8k Findstr command the Grep equivalent for Windows
+4.8k PHP : Extract part of a string starting from the middle
+13.5k Golang : How to get year, month and day?
+4.7k JavaScript: Add marker function on Google Map
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+28.7k Golang : Detect (OS) Operating System