io.SectionReader.ReadAt() Function in Golang with Examples
Last Updated :
03 May, 2020
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.ReadAt() function in Go language is used to return the number of bytes as read by
NewSectionReader method. This method holds a buffer and offsets as its parameters. 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) ReadAt(p []byte, off int64) (n int, err error)
Here, "s" is a pointer to the
SectionReader which is returned by the
NewSectionReader method, "p" is a buffer of stated byte length, and "off" is the offset of type int64 from where the reading starts.
Return value: It returns the number of bytes of the content returned from the stated buffer of specified length as well as offset 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:
C
// Golang program to illustrate the usage of
// io.SectionReader.ReadAt() 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\n")
// Calling NewSectionReader method with its parameters
r := io.NewSectionReader(reader, 6, 12)
// Defining buffer using make keyword
buf := make([]byte, 4)
// Calling ReadAt method with its parameters
n, err := r.ReadAt(buf, 2)
// 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: Geek
n: 4
In the above example first, content is returned from the NewSectionReader() then that content is read from the given offset in the ReadAt() method till the number of bytes stated in the buffer. Then the number of bytes of the resultant content is returned as output here. Moreover, the content of the buffer above has only 4 bytes so, "4" is returned and there is no error thrown while reading the stated content so the error is "nil".
Example 2:
C
// Golang program to illustrate the usage of
// io.SectionReader.ReadAt() 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("Computer Science!")
// Calling NewSectionReader method with its parameters
r := io.NewSectionReader(reader, 6, 12)
// Defining buffer using make keyword
buf := make([]byte, 10)
// Calling ReadAt method with its parameters
n, err := r.ReadAt(buf, 2)
// 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/sandbox798260752/prog.go:31 +0x263
Here, the content in the buffer used in the above code has fewer bytes than stated so, EOF error is thrown.
Similar Reads
Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google
2 min read
Go Programming Language (Introduction) Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env
11 min read
time.Sleep() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, t
3 min read
Golang Tutorial - Learn Go Programming Language This Golang tutorial provides you with all the insights into Go Language programming, Here we provide the basics, from how to install Golang to advanced concepts of Go programming with stable examples. So, if you are a professional and a beginner, this free Golang tutorial is the best place for your
10 min read
Top 10 Golang Project Ideas with Source Code in 2025 Golang, or Go, a programming language was created by Google. It's widely used for building different kinds of applications like websites and cloud services. The fastest way to master this language is by building projects related to it. This article introduces 10 beginner-friendly to medium-difficult
8 min read
Interfaces in Golang In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t
3 min read
strings.Contains Function in Golang with Examples strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax:Â func Contains(str, substr string) bool Here, str is the original string and substr is t
2 min read
fmt.Sprintf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. Moreover, this function is defined under the fmt package. Here, you
2 min read
Data Types in Go Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer
7 min read
Goroutines - Concurrency in Golang Goroutines in Go let functions run concurrently, using less memory than traditional threads. Every Go program starts with a main Goroutine, and if it exits, all others stop.Examplepackage mainimport "fmt"func display(str string) { for i := 0; i < 3; i++ { fmt.Println(str) }}func main() { go displ
2 min read