io.NewSectionReader() Function in Golang with Examples

In Go language, io packages supplies fundamental interfaces to the I/O primitives. And its principle job is to enclose the ongoing implementations of such king of primitives. The NewSectionReader() function in Go language is used to return a SectionReader that reads from the stated reader “r” which starts at the stated offset “off” and terminates with the EOF i.e, end of file after the given “n” number of bytes. Moreover, this function is defined under the io package. Here, you need to import the “io” package in order to use these functions.

Syntax:

func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader

Here, “r” is the reader from where the content is read, “off” is the stated offset from where the content reading starts, and “n” is the number of bytes till which the content is read.

Return value: It returns a “SectionReader” that reads from the stated reader “r” which starts at the stated offset “off” and terminates with the EOF i.e, end of file after the given “n” number of bytes.

Below examples illustrates the use of above method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.NewSectionReader() function
  
// Including main package
package main
  
// Importing fmt, io, strings, and os
import (
    "fmt"
    "io"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("Geeks\n")
  
    // Calling NewSectionReader method with its parameters
    r := io.NewSectionReader(reader, 3, 5)
  
    // Calling Copy method with its parameters
    Reader, err := io.Copy(os.Stdout, r)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("n: %v\n", Reader)
}

chevron_right


Output:

ks
n: 3

In the above example, Copy() method is used in order to return the output, and NewReader() method of strings is used from where the content to be read is written.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.NewSectionReader() function
  
// Including main package
package main
  
// Importing fmt, io, strings, and os
import (
    "fmt"
    "io"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("GeeksforGeeks\nis\na\nCS-Portal.\n")
  
    // Calling NewSectionReader method with its parameters
    r := io.NewSectionReader(reader, 7, 40)
  
    // Calling Copy method with its parameters
    Reader, err := io.Copy(os.Stdout, r)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("n: %v\n", Reader)
}

chevron_right


Output:

rGeeks
is
a
CS-Portal.
n: 23

Here, the content starts from the offset “7” and terminates after the number of bytes reaches “40”. But in the output returned here the content copied is of “23” bytes so “n” is 23.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.