To skip a certain number of bytes in a bufio.Reader
in Go, you can use the Reader.Discard()
method. Discard()
reads and discards the specified number of bytes from the underlying io.Reader
, advancing the reader's position.
Here's an example of how you can use Discard()
to skip a specific number of bytes in a bufio.Reader
:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Open the file for reading
file, err := os.Open("example.txt")
if err != nil {
fmt.Println("Failed to open file:", err)
return
}
defer file.Close()
// Create a bufio.Reader to read from the file
reader := bufio.NewReader(file)
// Skip the first 10 bytes
_, err = reader.Discard(10)
if err != nil {
fmt.Println("Failed to discard bytes:", err)
return
}
// Read and print the remaining content
remainingContent, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Failed to read remaining content:", err)
return
}
fmt.Println(remainingContent)
}
In this example, the Discard()
method is used to skip the first 10 bytes in the file "example.txt". After skipping the specified number of bytes, the remaining content is read and printed to the console.
Make sure to handle any errors that may occur during this process.