Golang io.SectionReader.ReadAt function example

package io

Golang io.SectionReader.ReadAt 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 at postion 2 into buff
 n, err := sectionReader.ReadAt(buff, 2)

 if err != nil {
 fmt.Println(err)
 }

 fmt.Printf("Read %d bytes of %s \n", n, string(buff[:]))

 }

Output :

EOF

Read 6 bytes of efghij

Reference :

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

Advertisement