Skip to content

Commit 0f0f8dd

Browse files
authored
Replacing the deprecated ioutil package (#569)
* replaced ioutil in .md files * replaced ioutil in .go files
1 parent 95e65cd commit 0f0f8dd

35 files changed

+61
-75
lines changed

command-line/v1/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package poker
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

command-line/v1/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package poker
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

command-line/v2/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package poker
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

command-line/v2/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package poker
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

command-line/v3/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package poker
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

command-line/v3/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package poker
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

context-aware-reader.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ They combined two simple abstractions (`context.Context` and `io.Reader`) to sol
2828

2929
Let's try and TDD some functionality so that we can wrap an `io.Reader` so it can be cancelled.
3030

31-
Testing this poses an interesting challenge. Normally when using an `io.Reader` you're usually supplying it to some other function and you don't really concern yourself with the details; such as `json.NewDecoder` or `ioutil.ReadAll`.
31+
Testing this poses an interesting challenge. Normally when using an `io.Reader` you're usually supplying it to some other function and you don't really concern yourself with the details; such as `json.NewDecoder` or `io.ReadAll`.
3232

3333
What we want to demonstrate is something like
3434

error-types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func DumbGetter(url string) (string, error) {
2424
}
2525

2626
defer res.Body.Close()
27-
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
27+
body, _ := io.ReadAll(res.Body) // ignoring err for brevity
2828

2929
return string(body), nil
3030
}

io.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ Let's create some helper functions which will create a temporary file with some
459459
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
460460
t.Helper()
461461

462-
tmpfile, err := ioutil.TempFile("", "db")
462+
tmpfile, err := os.CreateTemp("", "db")
463463

464464
if err != nil {
465465
t.Fatalf("could not create temp file %v", err)
@@ -483,7 +483,7 @@ func assertScoreEquals(t testing.TB, got, want int) {
483483
}
484484
```
485485

486-
[TempFile](https://golang.org/pkg/io/ioutil/#TempFile) creates a temporary file for us to use. The `"db"` value we've passed in is a prefix put on a random file name it will create. This is to ensure it won't clash with other files by accident.
486+
[CreateTemp](https://pkg.go.dev/os#CreateTemp) creates a temporary file for us to use. The `"db"` value we've passed in is a prefix put on a random file name it will create. This is to ensure it won't clash with other files by accident.
487487

488488
You'll notice we're not only returning our `ReadWriteSeeker` (the file) but also a function. We need to make sure that the file is removed once the test is finished. We don't want to leak details of the files into the test as it's prone to error and uninteresting for the reader. By returning a `removeFile` function, we can take care of the details in our helper and all the caller has to do is run `defer cleanDatabase()`.
489489

@@ -881,7 +881,7 @@ func TestTape_Write(t *testing.T) {
881881
tape.Write([]byte("abc"))
882882

883883
file.Seek(0, 0)
884-
newFileContents, _ := ioutil.ReadAll(file)
884+
newFileContents, _ := io.ReadAll(file)
885885

886886
got := string(newFileContents)
887887
want := "abc"

io/v3/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package main
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"testing"
87
)
98

109
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
1110
t.Helper()
1211

13-
tmpfile, err := ioutil.TempFile("", "db")
12+
tmpfile, err := os.CreateTemp("", "db")
1413

1514
if err != nil {
1615
t.Fatalf("could not create temp file %v", err)

io/v4/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package main
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"testing"
87
)
98

109
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
1110
t.Helper()
1211

13-
tmpfile, err := ioutil.TempFile("", "db")
12+
tmpfile, err := os.CreateTemp("", "db")
1413

1514
if err != nil {
1615
t.Fatalf("could not create temp file %v", err)

io/v5/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package main
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"testing"
87
)
98

109
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
1110
t.Helper()
1211

13-
tmpfile, err := ioutil.TempFile("", "db")
12+
tmpfile, err := os.CreateTemp("", "db")
1413

1514
if err != nil {
1615
t.Fatalf("could not create temp file %v", err)

io/v6/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package main
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"testing"
87
)
98

109
func createTempFile(t testing.TB, initialData string) (io.ReadWriteSeeker, func()) {
1110
t.Helper()
1211

13-
tmpfile, err := ioutil.TempFile("", "db")
12+
tmpfile, err := os.CreateTemp("", "db")
1413

1514
if err != nil {
1615
t.Fatalf("could not create temp file %v", err)

io/v7/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

io/v7/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

io/v8/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

io/v8/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

io/v9/file_system_store_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
109
t.Helper()
1110

12-
tmpfile, err := ioutil.TempFile("", "db")
11+
tmpfile, err := os.CreateTemp("", "db")
1312

1413
if err != nil {
1514
t.Fatalf("could not create temp file %v", err)

io/v9/tape_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66
)
77

@@ -14,7 +14,7 @@ func TestTape_Write(t *testing.T) {
1414
tape.Write([]byte("abc"))
1515

1616
file.Seek(0, 0)
17-
newFileContents, _ := ioutil.ReadAll(file)
17+
newFileContents, _ := io.ReadAll(file)
1818

1919
got := string(newFileContents)
2020
want := "abc"

os-exec.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func getXMLFromCommand() io.Reader {
9999
out, _ := cmd.StdoutPipe()
100100

101101
cmd.Start()
102-
data, _ := ioutil.ReadAll(out)
102+
data, _ := io.ReadAll(out)
103103
cmd.Wait()
104104

105105
return bytes.NewReader(data)

q-and-a/error-types/error-types_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package errortypes
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"testing"
@@ -30,7 +30,7 @@ func DumbGetter(url string) (string, error) {
3030
}
3131

3232
defer res.Body.Close()
33-
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
33+
body, _ := io.ReadAll(res.Body) // ignoring err for brevity
3434

3535
return string(body), nil
3636
}

q-and-a/error-types/v2/error-types_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package errortypes
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httptest"
99
"testing"
@@ -31,7 +31,7 @@ func DumbGetter(url string) (string, error) {
3131
}
3232

3333
defer res.Body.Close()
34-
body, _ := ioutil.ReadAll(res.Body) // ignoring err for brevity
34+
body, _ := io.ReadAll(res.Body) // ignoring err for brevity
3535

3636
return string(body), nil
3737
}

q-and-a/os-exec/os-exec_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/xml"
66
"io"
7-
"io/ioutil"
87
"os/exec"
98
"strings"
109
"testing"
@@ -25,7 +24,7 @@ func getXMLFromCommand() io.Reader {
2524
out, _ := cmd.StdoutPipe()
2625

2726
cmd.Start()
28-
data, _ := ioutil.ReadAll(out)
27+
data, _ := io.ReadAll(out)
2928
cmd.Wait()
3029

3130
return bytes.NewReader(data)

0 commit comments

Comments
 (0)