Standard library / os
os.File.Seek()
Moves the sequential I/O offset relative to start, current position or end. Append-mode behavior is unspecified. Example uses a disposable temporary workspace. Expected stdout: '2 c\n'.
Formal Syntax & Structure
go syntaxgo
func (f *File) Seek(offset int64, whence int) (ret int64, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| offset | int64 | Signed seek displacement. |
| whence | int | Seek origin. |
Return Value
(ret int64, err error)
Moves the sequential I/O offset relative to start, current position or end.
Working Example
Example Snippetgo
package main
import ( "fmt" "io" "os")
func check(e error) { if e != nil { panic(e) }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func opened() *os.File { seed(); f, e := os.OpenFile("a.txt", os.O_RDWR, 0); check(e); return f }func workspace() func() { root, e := os.MkdirTemp("", "bytemind-go-") check(e) old, e := os.Getwd() check(e) check(os.Chdir(root)) return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() { defer workspace()() f := opened() defer f.Close() p, e := f.Seek(-1, io.SeekEnd) check(e) b, e := io.ReadAll(f) check(e) fmt.Println(p, string(b))}