From ac94809b64eaa27f679ae33778c383fad3c3e9db Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 28 Aug 2021 06:38:35 -0700 Subject: [PATCH] Replace deprecated ioutil functions with others (#374) * Remove use of deprecated ioutil functions from examples * Remove usage of ioutil from gobyexample's own scripts + regenerate output --- examples/directories/directories.go | 9 ++++---- examples/directories/directories.hash | 4 ++-- examples/reading-files/reading-files.go | 3 +-- examples/reading-files/reading-files.hash | 4 ++-- .../spawning-processes/spawning-processes.go | 4 ++-- .../spawning-processes.hash | 4 ++-- .../temporary-files-and-directories.go | 17 +++++++------- .../temporary-files-and-directories.hash | 4 ++-- examples/writing-files/writing-files.go | 3 +-- examples/writing-files/writing-files.hash | 4 ++-- public/directories | 13 +++++------ public/reading-files | 7 +++--- public/spawning-processes | 8 +++---- public/temporary-files-and-directories | 21 ++++++++--------- public/writing-files | 7 +++--- tools/generate.go | 23 ++++++++++--------- tools/measure.go | 3 +-- 17 files changed, 65 insertions(+), 73 deletions(-) diff --git a/examples/directories/directories.go b/examples/directories/directories.go index 9b71274..ecc2fb5 100644 --- a/examples/directories/directories.go +++ b/examples/directories/directories.go @@ -5,7 +5,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" ) @@ -32,7 +31,7 @@ func main() { // Helper function to create a new empty file. createEmptyFile := func(name string) { d := []byte("") - check(ioutil.WriteFile(name, d, 0644)) + check(os.WriteFile(name, d, 0644)) } createEmptyFile("subdir/file1") @@ -48,8 +47,8 @@ func main() { createEmptyFile("subdir/parent/child/file4") // `ReadDir` lists directory contents, returning a - // slice of `os.FileInfo` objects. - c, err := ioutil.ReadDir("subdir/parent") + // slice of `os.DirEntry` objects. + c, err := os.ReadDir("subdir/parent") check(err) fmt.Println("Listing subdir/parent") @@ -64,7 +63,7 @@ func main() { // Now we'll see the contents of `subdir/parent/child` // when listing the *current* directory. - c, err = ioutil.ReadDir(".") + c, err = os.ReadDir(".") check(err) fmt.Println("Listing subdir/parent/child") diff --git a/examples/directories/directories.hash b/examples/directories/directories.hash index 7dae4aa..f62b81a 100644 --- a/examples/directories/directories.hash +++ b/examples/directories/directories.hash @@ -1,2 +1,2 @@ -fa3655fa8f4fa28e971cbe853dffb02773afce83 -UaeLMS5VQVR +d2eaefdc6dbeaf130e4824403baa948b5845c0ec +cICbVSX51zI diff --git a/examples/reading-files/reading-files.go b/examples/reading-files/reading-files.go index 49387a6..81edc19 100644 --- a/examples/reading-files/reading-files.go +++ b/examples/reading-files/reading-files.go @@ -8,7 +8,6 @@ import ( "bufio" "fmt" "io" - "io/ioutil" "os" ) @@ -24,7 +23,7 @@ func main() { // Perhaps the most basic file reading task is // slurping a file's entire contents into memory. - dat, err := ioutil.ReadFile("/tmp/dat") + dat, err := os.ReadFile("/tmp/dat") check(err) fmt.Print(string(dat)) diff --git a/examples/reading-files/reading-files.hash b/examples/reading-files/reading-files.hash index 2933d50..4250929 100644 --- a/examples/reading-files/reading-files.hash +++ b/examples/reading-files/reading-files.hash @@ -1,2 +1,2 @@ -3420958bafd67fd997481d1ada288566666343c7 -kF0cDC0drsX +5351edae47bb2f2c9b5d4b9682b8176beb0c24e5 +DF2Wo8nDKaF diff --git a/examples/spawning-processes/spawning-processes.go b/examples/spawning-processes/spawning-processes.go index 261a2d0..b4db3ab 100644 --- a/examples/spawning-processes/spawning-processes.go +++ b/examples/spawning-processes/spawning-processes.go @@ -9,7 +9,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "os/exec" ) @@ -46,7 +46,7 @@ func main() { grepCmd.Start() grepIn.Write([]byte("hello grep\ngoodbye grep")) grepIn.Close() - grepBytes, _ := ioutil.ReadAll(grepOut) + grepBytes, _ := io.ReadAll(grepOut) grepCmd.Wait() // We omitted error checks in the above example, but diff --git a/examples/spawning-processes/spawning-processes.hash b/examples/spawning-processes/spawning-processes.hash index 3c800a5..c0f815f 100644 --- a/examples/spawning-processes/spawning-processes.hash +++ b/examples/spawning-processes/spawning-processes.hash @@ -1,2 +1,2 @@ -6a62e3109c483c2b52a99905dc1ba5c8cb2a281b -m2CpSlHPEVq +b6e1e4b70a494be9f344a9f31aff520116d0ac24 +YJs_YtJY0sS diff --git a/examples/temporary-files-and-directories/temporary-files-and-directories.go b/examples/temporary-files-and-directories/temporary-files-and-directories.go index c41d230..8907f71 100644 --- a/examples/temporary-files-and-directories/temporary-files-and-directories.go +++ b/examples/temporary-files-and-directories/temporary-files-and-directories.go @@ -8,7 +8,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" ) @@ -22,17 +21,17 @@ func check(e error) { func main() { // The easiest way to create a temporary file is by - // calling `ioutil.TempFile`. It creates a file *and* + // calling `os.CreateTemp`. It creates a file *and* // opens it for reading and writing. We provide `""` - // as the first argument, so `ioutil.TempFile` will + // as the first argument, so `os.CreateTemp` will // create the file in the default location for our OS. - f, err := ioutil.TempFile("", "sample") + f, err := os.CreateTemp("", "sample") check(err) // Display the name of the temporary file. On // Unix-based OSes the directory will likely be `/tmp`. // The file name starts with the prefix given as the - // second argument to `ioutil.TempFile` and the rest + // second argument to `os.CreateTemp` and the rest // is chosen automatically to ensure that concurrent // calls will always create different file names. fmt.Println("Temp file name:", f.Name()) @@ -49,10 +48,10 @@ func main() { // If we intend to write many temporary files, we may // prefer to create a temporary *directory*. - // `ioutil.TempDir`'s arguments are the same as - // `TempFile`'s, but it returns a directory *name* + // `os.MkdirTemp`'s arguments are the same as + // `CreateTemp`'s, but it returns a directory *name* // rather than an open file. - dname, err := ioutil.TempDir("", "sampledir") + dname, err := os.MkdirTemp("", "sampledir") check(err) fmt.Println("Temp dir name:", dname) @@ -61,6 +60,6 @@ func main() { // Now we can synthesize temporary file names by // prefixing them with our temporary directory. fname := filepath.Join(dname, "file1") - err = ioutil.WriteFile(fname, []byte{1, 2}, 0666) + err = os.WriteFile(fname, []byte{1, 2}, 0666) check(err) } diff --git a/examples/temporary-files-and-directories/temporary-files-and-directories.hash b/examples/temporary-files-and-directories/temporary-files-and-directories.hash index cf6faa7..2bc959f 100644 --- a/examples/temporary-files-and-directories/temporary-files-and-directories.hash +++ b/examples/temporary-files-and-directories/temporary-files-and-directories.hash @@ -1,2 +1,2 @@ -cc4755e23cb4ba3c0e0ef5554ec9e9477372422a -nMpjCsALS6P +d875fd8f061e895d72c48c360a8ad4b04e406dd4 +hVcPg9RH3_V diff --git a/examples/writing-files/writing-files.go b/examples/writing-files/writing-files.go index ef8bd06..e8f3d77 100644 --- a/examples/writing-files/writing-files.go +++ b/examples/writing-files/writing-files.go @@ -6,7 +6,6 @@ package main import ( "bufio" "fmt" - "io/ioutil" "os" ) @@ -21,7 +20,7 @@ func main() { // To start, here's how to dump a string (or just // bytes) into a file. d1 := []byte("hello\ngo\n") - err := ioutil.WriteFile("/tmp/dat1", d1, 0644) + err := os.WriteFile("/tmp/dat1", d1, 0644) check(err) // For more granular writes, open a file for writing. diff --git a/examples/writing-files/writing-files.hash b/examples/writing-files/writing-files.hash index 1513200..fcac248 100644 --- a/examples/writing-files/writing-files.hash +++ b/examples/writing-files/writing-files.hash @@ -1,2 +1,2 @@ -314a0074840e22b328b6412130c17b9bea53c9c9 -fQ7sd4gXv0F +e312100df0c063ecd65e7599653636188277b6a6 +Y12O-L_zFS1 diff --git a/public/directories b/public/directories index 45efcea..8c725ae 100644 --- a/public/directories +++ b/public/directories @@ -42,7 +42,7 @@ - +
package main
 
@@ -56,7 +56,6 @@
import (
     "fmt"
-    "io/ioutil"
     "os"
     "path/filepath"
 )
@@ -131,7 +130,7 @@ will delete a whole directory tree (similarly to
           
     createEmptyFile := func(name string) {
         d := []byte("")
-        check(ioutil.WriteFile(name, d, 0644))
+        check(os.WriteFile(name, d, 0644))
     }
 
@@ -180,13 +179,13 @@ command-line mkdir -p.

ReadDir lists directory contents, returning a -slice of os.FileInfo objects.

+slice of os.DirEntry objects.

-    c, err := ioutil.ReadDir("subdir/parent")
+    c, err := os.ReadDir("subdir/parent")
     check(err)
 
@@ -230,7 +229,7 @@ when listing the current directory.

-    c, err = ioutil.ReadDir(".")
+    c, err = os.ReadDir(".")
     check(err)
 
@@ -343,7 +342,7 @@ recursively by filepath.Walk.

diff --git a/public/reading-files b/public/reading-files index 0c5f12e..94100f8 100644 --- a/public/reading-files +++ b/public/reading-files @@ -43,7 +43,7 @@ reading files.

- +
package main
 
@@ -59,7 +59,6 @@ reading files.

"bufio" "fmt" "io" - "io/ioutil" "os" )
@@ -104,7 +103,7 @@ slurping a file’s entire contents into memory.

-    dat, err := ioutil.ReadFile("/tmp/dat")
+    dat, err := os.ReadFile("/tmp/dat")
     check(err)
     fmt.Print(string(dat))
 
@@ -282,7 +281,7 @@ be scheduled immediately after Opening with diff --git a/public/spawning-processes b/public/spawning-processes index dfc10dd..c5781ec 100644 --- a/public/spawning-processes +++ b/public/spawning-processes @@ -46,7 +46,7 @@ of spawning processes from Go.

- +
package main
 
@@ -60,7 +60,7 @@ of spawning processes from Go.

import (
     "fmt"
-    "io/ioutil"
+    "io"
     "os/exec"
 )
 
@@ -146,7 +146,7 @@ to exit.

grepCmd.Start() grepIn.Write([]byte("hello grep\ngoodbye grep")) grepIn.Close() - grepBytes, _ := ioutil.ReadAll(grepOut) + grepBytes, _ := io.ReadAll(grepOut) grepCmd.Wait() @@ -251,7 +251,7 @@ as if we had run them directly from the command-line.

diff --git a/public/temporary-files-and-directories b/public/temporary-files-and-directories index 9ee4aba..7d3cbaa 100644 --- a/public/temporary-files-and-directories +++ b/public/temporary-files-and-directories @@ -45,7 +45,7 @@ time.

- +
package main
 
@@ -59,7 +59,6 @@ time.

import (
     "fmt"
-    "io/ioutil"
     "os"
     "path/filepath"
 )
@@ -96,16 +95,16 @@ time.

The easiest way to create a temporary file is by -calling ioutil.TempFile. It creates a file and +calling os.CreateTemp. It creates a file and opens it for reading and writing. We provide "" -as the first argument, so ioutil.TempFile will +as the first argument, so os.CreateTemp will create the file in the default location for our OS.

-    f, err := ioutil.TempFile("", "sample")
+    f, err := os.CreateTemp("", "sample")
     check(err)
 
@@ -116,7 +115,7 @@ create the file in the default location for our OS.

Display the name of the temporary file. On Unix-based OSes the directory will likely be /tmp. The file name starts with the prefix given as the -second argument to ioutil.TempFile and the rest +second argument to os.CreateTemp and the rest is chosen automatically to ensure that concurrent calls will always create different file names.

@@ -163,15 +162,15 @@ explicitly.

If we intend to write many temporary files, we may prefer to create a temporary directory. -ioutil.TempDir’s arguments are the same as -TempFile’s, but it returns a directory name +os.MkdirTemp’s arguments are the same as +CreateTemp’s, but it returns a directory name rather than an open file.

-    dname, err := ioutil.TempDir("", "sampledir")
+    dname, err := os.MkdirTemp("", "sampledir")
     check(err)
     fmt.Println("Temp dir name:", dname)
 
@@ -199,7 +198,7 @@ prefixing them with our temporary directory.

     fname := filepath.Join(dname, "file1")
-    err = ioutil.WriteFile(fname, []byte{1, 2}, 0666)
+    err = os.WriteFile(fname, []byte{1, 2}, 0666)
     check(err)
 }
 
@@ -235,7 +234,7 @@ prefixing them with our temporary directory.

diff --git a/public/writing-files b/public/writing-files index 5591288..d26e7d5 100644 --- a/public/writing-files +++ b/public/writing-files @@ -42,7 +42,7 @@ ones we saw earlier for reading.

- +
package main
 
@@ -57,7 +57,6 @@ ones we saw earlier for reading.

import (
     "bufio"
     "fmt"
-    "io/ioutil"
     "os"
 )
 
@@ -100,7 +99,7 @@ bytes) into a file.

     d1 := []byte("hello\ngo\n")
-    err := ioutil.WriteFile("/tmp/dat1", d1, 0644)
+    err := os.WriteFile("/tmp/dat1", d1, 0644)
     check(err)
 
@@ -282,7 +281,7 @@ we’ve just seen to the stdin and stdout streams. diff --git a/tools/generate.go b/tools/generate.go index ca4aabb..c45a0a6 100644 --- a/tools/generate.go +++ b/tools/generate.go @@ -4,11 +4,7 @@ import ( "bytes" "crypto/sha1" "fmt" - "github.com/alecthomas/chroma" - "github.com/alecthomas/chroma/formatters/html" - "github.com/alecthomas/chroma/lexers" - "github.com/alecthomas/chroma/styles" - "io/ioutil" + "io" "net/http" "os" "os/exec" @@ -17,6 +13,11 @@ import ( "strings" "text/template" + "github.com/alecthomas/chroma" + "github.com/alecthomas/chroma/formatters/html" + "github.com/alecthomas/chroma/lexers" + "github.com/alecthomas/chroma/styles" + "github.com/russross/blackfriday/v2" ) @@ -41,9 +42,9 @@ func ensureDir(dir string) { } func copyFile(src, dst string) { - dat, err := ioutil.ReadFile(src) + dat, err := os.ReadFile(src) check(err) - err = ioutil.WriteFile(dst, dat, 0644) + err = os.WriteFile(dst, dat, 0644) check(err) } @@ -59,7 +60,7 @@ func pipe(bin string, arg []string, src string) []byte { check(err) err = in.Close() check(err) - bytes, err := ioutil.ReadAll(out) + bytes, err := io.ReadAll(out) check(err) err = cmd.Wait() check(err) @@ -74,7 +75,7 @@ func sha1Sum(s string) string { } func mustReadFile(path string) string { - bytes, err := ioutil.ReadFile(path) + bytes, err := os.ReadFile(path) check(err) return string(bytes) } @@ -141,11 +142,11 @@ func resetURLHashFile(codehash, code, sourcePath string) string { resp, err := http.Post("https://play.golang.org/share", "text/plain", payload) check(err) defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) check(err) urlkey := string(body) data := fmt.Sprintf("%s\n%s\n", codehash, urlkey) - ioutil.WriteFile(sourcePath, []byte(data), 0644) + os.WriteFile(sourcePath, []byte(data), 0644) return urlkey } diff --git a/tools/measure.go b/tools/measure.go index eb26378..7c9d6be 100644 --- a/tools/measure.go +++ b/tools/measure.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -17,7 +16,7 @@ func check(err error) { } func readLines(path string) []string { - srcBytes, err := ioutil.ReadFile(path) + srcBytes, err := os.ReadFile(path) check(err) return strings.Split(string(srcBytes), "\n") }