Seeking a position within a file
In some cases, you need to read from or write to a particular location in a file, such as an indexed file. The recipe will show you how to use the position seeking in the context of flat file operations.
How to do it...
- Open the console and create the folder
chapter05/recipe06
. - Navigate to the directory.
- Create the file
flatfile.txt
with the following content:
123.Jun.......Wong...... 12..Novak.....Jurgen.... 10..Thomas....Sohlich...
- Create the
fileseek.go
file with the following content:
package main import ( "errors" "fmt" "os" ) const lineLegth = 25 func main() { f, e := os.OpenFile("flatfile.txt", os.O_RDWR|os.O_CREATE, os.ModePerm) if e != nil { panic(e) } defer f.Close() fmt.Println(readRecords(2, "last", f)) if err := writeRecord(2, "first", "Radomir", f); err !=...