Golang io.SectionReader.Read function example
package io
Golang io.SectionReader.Read 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, 2, 8)
buff := make([]byte, 7)
// read section into buff
n, err := sectionReader.Read(buff) // -- here!
if err != nil {
fmt.Println(err)
}
fmt.Printf("Read %d bytes of %s \n", n, string(buff[:]))
}
Output :
Read 7 bytes of cdefghi
Reference :
Advertisement
Something interesting
Tutorials
+4.9k HTTP common errors and their meaning explained
+11.6k Golang : Convert(cast) float to int
+13.9k Golang : How to determine if a year is leap year?
+16.7k Golang : Gzip file example
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+10.1k Golang : Check a web page existence with HEAD request example
+9k Golang : Go as a script or running go with shebang/hashbang style
+4k Detect if Google Analytics and Developer Media are loaded properly or not
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+13.2k Golang : Convert(cast) int to int64
+23.6k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+10.3k Golang : Embed secret text string into binary(executable) file