Remove use of deprecated ioutil functions from examples

This commit is contained in:
Eli Bendersky 2021-06-01 06:15:42 -07:00
parent 58e66a8103
commit bb0b9bbea9
5 changed files with 16 additions and 20 deletions

View File

@ -5,7 +5,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -32,7 +31,7 @@ func main() {
// Helper function to create a new empty file. // Helper function to create a new empty file.
createEmptyFile := func(name string) { createEmptyFile := func(name string) {
d := []byte("") d := []byte("")
check(ioutil.WriteFile(name, d, 0644)) check(os.WriteFile(name, d, 0644))
} }
createEmptyFile("subdir/file1") createEmptyFile("subdir/file1")
@ -48,8 +47,8 @@ func main() {
createEmptyFile("subdir/parent/child/file4") createEmptyFile("subdir/parent/child/file4")
// `ReadDir` lists directory contents, returning a // `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) check(err)
fmt.Println("Listing subdir/parent") fmt.Println("Listing subdir/parent")
@ -64,7 +63,7 @@ func main() {
// Now we'll see the contents of `subdir/parent/child` // Now we'll see the contents of `subdir/parent/child`
// when listing the *current* directory. // when listing the *current* directory.
c, err = ioutil.ReadDir(".") c, err = os.ReadDir(".")
check(err) check(err)
fmt.Println("Listing subdir/parent/child") fmt.Println("Listing subdir/parent/child")

View File

@ -8,7 +8,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
) )
@ -24,7 +23,7 @@ func main() {
// Perhaps the most basic file reading task is // Perhaps the most basic file reading task is
// slurping a file's entire contents into memory. // slurping a file's entire contents into memory.
dat, err := ioutil.ReadFile("/tmp/dat") dat, err := os.ReadFile("/tmp/dat")
check(err) check(err)
fmt.Print(string(dat)) fmt.Print(string(dat))

View File

@ -9,7 +9,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os/exec" "os/exec"
) )
@ -46,7 +46,7 @@ func main() {
grepCmd.Start() grepCmd.Start()
grepIn.Write([]byte("hello grep\ngoodbye grep")) grepIn.Write([]byte("hello grep\ngoodbye grep"))
grepIn.Close() grepIn.Close()
grepBytes, _ := ioutil.ReadAll(grepOut) grepBytes, _ := io.ReadAll(grepOut)
grepCmd.Wait() grepCmd.Wait()
// We omitted error checks in the above example, but // We omitted error checks in the above example, but

View File

@ -8,7 +8,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -22,17 +21,17 @@ func check(e error) {
func main() { func main() {
// The easiest way to create a temporary file is by // 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 `""` // 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. // create the file in the default location for our OS.
f, err := ioutil.TempFile("", "sample") f, err := os.CreateTemp("", "sample")
check(err) check(err)
// Display the name of the temporary file. On // Display the name of the temporary file. On
// Unix-based OSes the directory will likely be `/tmp`. // Unix-based OSes the directory will likely be `/tmp`.
// The file name starts with the prefix given as the // 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 // is chosen automatically to ensure that concurrent
// calls will always create different file names. // calls will always create different file names.
fmt.Println("Temp file name:", f.Name()) fmt.Println("Temp file name:", f.Name())
@ -49,10 +48,10 @@ func main() {
// If we intend to write many temporary files, we may // If we intend to write many temporary files, we may
// prefer to create a temporary *directory*. // prefer to create a temporary *directory*.
// `ioutil.TempDir`'s arguments are the same as // `os.MkdirTemp`'s arguments are the same as
// `TempFile`'s, but it returns a directory *name* // `CreateTemp`'s, but it returns a directory *name*
// rather than an open file. // rather than an open file.
dname, err := ioutil.TempDir("", "sampledir") dname, err := os.MkdirTemp("", "sampledir")
check(err) check(err)
fmt.Println("Temp dir name:", dname) fmt.Println("Temp dir name:", dname)
@ -61,6 +60,6 @@ func main() {
// Now we can synthesize temporary file names by // Now we can synthesize temporary file names by
// prefixing them with our temporary directory. // prefixing them with our temporary directory.
fname := filepath.Join(dname, "file1") fname := filepath.Join(dname, "file1")
err = ioutil.WriteFile(fname, []byte{1, 2}, 0666) err = os.WriteFile(fname, []byte{1, 2}, 0666)
check(err) check(err)
} }

View File

@ -6,7 +6,6 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io/ioutil"
"os" "os"
) )
@ -21,7 +20,7 @@ func main() {
// To start, here's how to dump a string (or just // To start, here's how to dump a string (or just
// bytes) into a file. // bytes) into a file.
d1 := []byte("hello\ngo\n") d1 := []byte("hello\ngo\n")
err := ioutil.WriteFile("/tmp/dat1", d1, 0644) err := os.WriteFile("/tmp/dat1", d1, 0644)
check(err) check(err)
// For more granular writes, open a file for writing. // For more granular writes, open a file for writing.