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 :

http://golang.org/pkg/io/#SectionReader.Read

Advertisement