-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSeekFile.go
46 lines (39 loc) · 994 Bytes
/
SeekFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"os"
"fmt"
"log"
)
func main() {
file, _ := os.Open("test.txt")
defer file.Close()
// Offset is how many bytes to move
// Offset can be positive or negative
var offset int64 = 5
// Whence is the point of reference for offset
// 0 = Beginning of file
// 1 = Current position
// 2 = End of file
var whence int = 0
newPosition, err := file.Seek(offset, whence)
if err != nil {
log.Fatal(err)
}
fmt.Println("Just moved to 5:", newPosition)
// Go back 2 bytes from current position
newPosition, err = file.Seek(-2, 1)
if err != nil {
log.Fatal(err)
}
fmt.Println("Just moved back two:", newPosition)
// Find the current position by getting the
// return value from Seek after moving 0 bytes
currentPosition, err := file.Seek(0, 1)
fmt.Println("Current position:", currentPosition)
// Go to beginning of file
newPosition, err = file.Seek(0, 0)
if err != nil {
log.Fatal(err)
}
fmt.Println("Position after seeking 0,0:", newPosition)
}