Skip to content

Commit 22780a6

Browse files
authored
use whence constant for Seek() (quii#746)
1 parent 2746e89 commit 22780a6

40 files changed

+91
-69
lines changed

command-line/v1/file_system_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package poker
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
78
"sort"
89
)
@@ -35,7 +36,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
3536
}
3637

3738
func initialisePlayerDBFile(file *os.File) error {
38-
file.Seek(0, 0)
39+
file.Seek(0, io.SeekStart)
3940

4041
info, err := file.Stat()
4142

@@ -45,7 +46,7 @@ func initialisePlayerDBFile(file *os.File) error {
4546

4647
if info.Size() == 0 {
4748
file.Write([]byte("[]"))
48-
file.Seek(0, 0)
49+
file.Seek(0, io.SeekStart)
4950
}
5051

5152
return nil

command-line/v1/tape.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package poker
22

33
import (
4+
"io"
45
"os"
56
)
67

@@ -10,6 +11,6 @@ type tape struct {
1011

1112
func (t *tape) Write(p []byte) (n int, err error) {
1213
t.file.Truncate(0)
13-
t.file.Seek(0, 0)
14+
t.file.Seek(0, io.SeekStart)
1415
return t.file.Write(p)
1516
}

command-line/v1/tape_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {
1313

1414
tape.Write([]byte("abc"))
1515

16-
file.Seek(0, 0)
16+
file.Seek(0, io.SeekStart)
1717
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)

command-line/v2/file_system_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package poker
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
78
"sort"
89
)
@@ -35,7 +36,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
3536
}
3637

3738
func initialisePlayerDBFile(file *os.File) error {
38-
file.Seek(0, 0)
39+
file.Seek(0, io.SeekStart)
3940

4041
info, err := file.Stat()
4142

@@ -45,7 +46,7 @@ func initialisePlayerDBFile(file *os.File) error {
4546

4647
if info.Size() == 0 {
4748
file.Write([]byte("[]"))
48-
file.Seek(0, 0)
49+
file.Seek(0, io.SeekStart)
4950
}
5051

5152
return nil

command-line/v2/tape.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package poker
22

33
import (
4+
"io"
45
"os"
56
)
67

@@ -10,6 +11,6 @@ type tape struct {
1011

1112
func (t *tape) Write(p []byte) (n int, err error) {
1213
t.file.Truncate(0)
13-
t.file.Seek(0, 0)
14+
t.file.Seek(0, io.SeekStart)
1415
return t.file.Write(p)
1516
}

command-line/v2/tape_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {
1313

1414
tape.Write([]byte("abc"))
1515

16-
file.Seek(0, 0)
16+
file.Seek(0, io.SeekStart)
1717
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)

command-line/v3/file_system_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package poker
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os"
78
"sort"
89
)
@@ -57,7 +58,7 @@ func FileSystemPlayerStoreFromFile(path string) (*FileSystemPlayerStore, func(),
5758
}
5859

5960
func initialisePlayerDBFile(file *os.File) error {
60-
file.Seek(0, 0)
61+
file.Seek(0, io.SeekStart)
6162

6263
info, err := file.Stat()
6364

@@ -67,7 +68,7 @@ func initialisePlayerDBFile(file *os.File) error {
6768

6869
if info.Size() == 0 {
6970
file.Write([]byte("[]"))
70-
file.Seek(0, 0)
71+
file.Seek(0, io.SeekStart)
7172
}
7273

7374
return nil

command-line/v3/tape.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package poker
22

33
import (
4+
"io"
45
"os"
56
)
67

@@ -10,6 +11,6 @@ type tape struct {
1011

1112
func (t *tape) Write(p []byte) (n int, err error) {
1213
t.file.Truncate(0)
13-
t.file.Seek(0, 0)
14+
t.file.Seek(0, io.SeekStart)
1415
return t.file.Write(p)
1516
}

command-line/v3/tape_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {
1313

1414
tape.Write([]byte("abc"))
1515

16-
file.Seek(0, 0)
16+
file.Seek(0, io.SeekStart)
1717
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)

io.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ type FileSystemPlayerStore struct {
319319
}
320320

321321
func (f *FileSystemPlayerStore) GetLeague() []Player {
322-
f.database.Seek(0, 0)
322+
f.database.Seek(0, io.SeekStart)
323323
league, _ := NewLeague(f.database)
324324
return league
325325
}
@@ -586,7 +586,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
586586
}
587587
}
588588

589-
f.database.Seek(0, 0)
589+
f.database.Seek(0, io.SeekStart)
590590
json.NewEncoder(f.database).Encode(league)
591591
}
592592
```
@@ -646,7 +646,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
646646
player.Wins++
647647
}
648648

649-
f.database.Seek(0, 0)
649+
f.database.Seek(0, io.SeekStart)
650650
json.NewEncoder(f.database).Encode(league)
651651
}
652652
```
@@ -699,7 +699,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
699699
league = append(league, Player{name, 1})
700700
}
701701

702-
f.database.Seek(0, 0)
702+
f.database.Seek(0, io.SeekStart)
703703
json.NewEncoder(f.database).Encode(league)
704704
}
705705
```
@@ -765,7 +765,7 @@ type FileSystemPlayerStore struct {
765765
}
766766

767767
func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
768-
database.Seek(0, 0)
768+
database.Seek(0, io.SeekStart)
769769
league, _ := NewLeague(database)
770770
return &FileSystemPlayerStore{
771771
database: database,
@@ -802,7 +802,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
802802
f.league = append(f.league, Player{name, 1})
803803
}
804804

805-
f.database.Seek(0, 0)
805+
f.database.Seek(0, io.SeekStart)
806806
json.NewEncoder(f.database).Encode(f.league)
807807
}
808808
```
@@ -832,7 +832,7 @@ type tape struct {
832832
}
833833

834834
func (t *tape) Write(p []byte) (n int, err error) {
835-
t.file.Seek(0, 0)
835+
t.file.Seek(0, io.SeekStart)
836836
return t.file.Write(p)
837837
}
838838
```
@@ -852,7 +852,7 @@ Update the constructor to use `Tape`
852852
```go
853853
//file_system_store.go
854854
func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
855-
database.Seek(0, 0)
855+
database.Seek(0, io.SeekStart)
856856
league, _ := NewLeague(database)
857857

858858
return &FileSystemPlayerStore{
@@ -880,7 +880,7 @@ func TestTape_Write(t *testing.T) {
880880

881881
tape.Write([]byte("abc"))
882882

883-
file.Seek(0, 0)
883+
file.Seek(0, io.SeekStart)
884884
newFileContents, _ := io.ReadAll(file)
885885

886886
got := string(newFileContents)
@@ -916,7 +916,7 @@ type tape struct {
916916

917917
func (t *tape) Write(p []byte) (n int, err error) {
918918
t.file.Truncate(0)
919-
t.file.Seek(0, 0)
919+
t.file.Seek(0, io.SeekStart)
920920
return t.file.Write(p)
921921
}
922922
```
@@ -941,7 +941,7 @@ type FileSystemPlayerStore struct {
941941
}
942942

943943
func NewFileSystemPlayerStore(file *os.File) *FileSystemPlayerStore {
944-
file.Seek(0, 0)
944+
file.Seek(0, io.SeekStart)
945945
league, _ := NewLeague(file)
946946

947947
return &FileSystemPlayerStore{
@@ -1011,7 +1011,7 @@ Let's make it so our constructor is capable of returning an error.
10111011
```go
10121012
//file_system_store.go
10131013
func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
1014-
file.Seek(0, 0)
1014+
file.Seek(0, io.SeekStart)
10151015
league, err := NewLeague(file)
10161016

10171017
if err != nil {
@@ -1121,7 +1121,7 @@ Change our constructor to the following
11211121
//file_system_store.go
11221122
func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
11231123

1124-
file.Seek(0, 0)
1124+
file.Seek(0, io.SeekStart)
11251125

11261126
info, err := file.Stat()
11271127

@@ -1131,7 +1131,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
11311131

11321132
if info.Size() == 0 {
11331133
file.Write([]byte("[]"))
1134-
file.Seek(0, 0)
1134+
file.Seek(0, io.SeekStart)
11351135
}
11361136

11371137
league, err := NewLeague(file)
@@ -1156,7 +1156,7 @@ Our constructor is a bit messy now, so let's extract the initialise code into a
11561156
```go
11571157
//file_system_store.go
11581158
func initialisePlayerDBFile(file *os.File) error {
1159-
file.Seek(0, 0)
1159+
file.Seek(0, io.SeekStart)
11601160

11611161
info, err := file.Stat()
11621162

@@ -1166,7 +1166,7 @@ func initialisePlayerDBFile(file *os.File) error {
11661166

11671167
if info.Size() == 0 {
11681168
file.Write([]byte("[]"))
1169-
file.Seek(0, 0)
1169+
file.Seek(0, io.SeekStart)
11701170
}
11711171

11721172
return nil

io/v1/file_system_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type FileSystemPlayerStore struct {
1111

1212
// GetLeague returns the scores of all the players.
1313
func (f *FileSystemPlayerStore) GetLeague() []Player {
14-
f.database.Seek(0, 0)
14+
f.database.Seek(0, io.SeekStart)
1515
league, _ := NewLeague(f.database)
1616
return league
1717
}

io/v2/file_system_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type FileSystemPlayerStore struct {
1111

1212
// GetLeague returns the scores of all the players.
1313
func (f *FileSystemPlayerStore) GetLeague() []Player {
14-
f.database.Seek(0, 0)
14+
f.database.Seek(0, io.SeekStart)
1515
league, _ := NewLeague(f.database)
1616
return league
1717
}

io/v3/file_system_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {
1212

1313
// GetLeague returns the scores of all the players.
1414
func (f *FileSystemPlayerStore) GetLeague() League {
15-
f.database.Seek(0, 0)
15+
f.database.Seek(0, io.SeekStart)
1616
league, _ := NewLeague(f.database)
1717
return league
1818
}
@@ -38,6 +38,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
3838
player.Wins++
3939
}
4040

41-
f.database.Seek(0, 0)
41+
f.database.Seek(0, io.SeekStart)
4242
json.NewEncoder(f.database).Encode(league)
4343
}

io/v4/file_system_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {
1212

1313
// GetLeague returns the scores of all the players.
1414
func (f *FileSystemPlayerStore) GetLeague() League {
15-
f.database.Seek(0, 0)
15+
f.database.Seek(0, io.SeekStart)
1616
league, _ := NewLeague(f.database)
1717
return league
1818
}
@@ -40,6 +40,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
4040
league = append(league, Player{name, 1})
4141
}
4242

43-
f.database.Seek(0, 0)
43+
f.database.Seek(0, io.SeekStart)
4444
json.NewEncoder(f.database).Encode(league)
4545
}

io/v5/file_system_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {
1212

1313
// GetLeague returns the scores of all the players.
1414
func (f *FileSystemPlayerStore) GetLeague() League {
15-
f.database.Seek(0, 0)
15+
f.database.Seek(0, io.SeekStart)
1616
league, _ := NewLeague(f.database)
1717
return league
1818
}
@@ -40,6 +40,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
4040
league = append(league, Player{name, 1})
4141
}
4242

43-
f.database.Seek(0, 0)
43+
f.database.Seek(0, io.SeekStart)
4444
json.NewEncoder(f.database).Encode(league)
4545
}

io/v6/file_system_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type FileSystemPlayerStore struct {
1313

1414
// NewFileSystemPlayerStore creates a FileSystemPlayerStore.
1515
func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
16-
database.Seek(0, 0)
16+
database.Seek(0, io.SeekStart)
1717
league, _ := NewLeague(database)
1818

1919
return &FileSystemPlayerStore{
@@ -49,6 +49,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
4949
f.league = append(f.league, Player{name, 1})
5050
}
5151

52-
f.database.Seek(0, 0)
52+
f.database.Seek(0, io.SeekStart)
5353
json.NewEncoder(f.database).Encode(f.league)
5454
}

io/v7/file_system_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"io"
56
"os"
67
)
78

@@ -13,7 +14,7 @@ type FileSystemPlayerStore struct {
1314

1415
// NewFileSystemPlayerStore creates a FileSystemPlayerStore.
1516
func NewFileSystemPlayerStore(file *os.File) *FileSystemPlayerStore {
16-
file.Seek(0, 0)
17+
file.Seek(0, io.SeekStart)
1718
league, _ := NewLeague(file)
1819

1920
return &FileSystemPlayerStore{

0 commit comments

Comments
 (0)