Open In App

io.SectionReader.Read() Function in Golang with Examples

Last Updated : 03 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The SectionReader.Read() function in Go language is used to return the number of bytes as read by NewSectionReader method. This method holds a buffer as its parameter. 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 (s *SectionReader) Read(p []byte) (n int, err error)

Here, “s” is a pointer to the SectionReader which is returned by the NewSectionReader method, and “p” is a buffer of stated byte length.

Return value: It returns the number of bytes of the content returned from the stated buffer of specified length and also returns an error if any but if no error occurred then “nil” is returned.

Below examples illustrates the use of above method:

Example 1:




// Golang program to illustrate the usage of
// io.SectionReader.Read() function
  
// Including main package
package main
  
// Importing fmt, io, and strings
import (
    "fmt"
    "io"
    "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, 2, 4)
  
    // Defining buffer using make keyword
    buf := make([]byte, 3)
  
    // Calling Read method with its parameter
    n, err := r.Read(buf)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("Content in buffer: %s\n", buf)
    fmt.Printf("n: %v\n", n)
}


Output:

Content in buffer: eks
n: 3

In the above example, the content of the buffer has only three bytes so, “3” is returned and there is no error thrown while reading the stated content so the error is “nil”.

Example 2:




// Golang program to illustrate the usage of
// io.SectionReader.Read() function
  
// Including main package
package main
  
// Importing fmt, io, and strings
import (
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("GeeksforGeeks\nis\na\nCS-Portal.")
  
    // Calling NewSectionReader method with its parameters
    r := io.NewSectionReader(reader, 6, 34)
  
    // Defining buffer using make keyword
    buf := make([]byte, 25)
  
    // Calling Read method with its parameter
    n, err := r.Read(buf)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("Content in buffer: %s\n", buf)
    fmt.Printf("n: %v\n", n)
}


Output:

panic: EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox125171693/prog.go:31 +0x25a

Here, the content in the buffer used in above code has less number of bytes than stated so, EOF error is thrown.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads