io.SectionReader.Size() Function in Golang with Examples
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.Size() function in Go language is used to find the size of the section that is returned by the NewSectionReader() method in 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 (s *SectionReader) Size() int64
Here, “s” is a pointer to the SectionReader which is returned by the NewSectionReader method.
Return value: It returns the size of the section returned in bytes and is of type int64.
Example 1:
// Golang program to illustrate the usage of // io.SectionReader.Size() function // Including the 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( "Beginner" ) // Calling NewSectionReader method with its parameters r := io.NewSectionReader(reader, 1, 4) // Calling Size method size := r.Size() // Prints output fmt.Printf( "The size of the section in bytes is: %v\n" , size) } |
Output:
The size of the section in bytes is: 4
Example 2:
// Golang program to illustrate the usage of // io.SectionReader.Size() 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( "w3wiki\nis\na\nCS-Portal.\n" ) // Calling NewSectionReader method with its parameters r := io.NewSectionReader(reader, 5, 23) // Calling Size method size := r.Size() // Prints output fmt.Printf( "The size of the section in bytes is: %v\n" , size) } |
Output:
The size of the section in bytes is: 23