Skip to content

all: replace io/ioutil with io and os package #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cases/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"reflect"
"strconv"
Expand Down Expand Up @@ -635,9 +634,9 @@ func genTablesTest() {

// We discard the output as we know we have perfect functions. We run them
// just to verify the properties are correct.
n := printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased)
n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower)
n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper)
n := printProperties(io.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased)
n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower)
n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper)
if n > 0 {
log.Fatalf("One of the discarded properties does not have a perfect filter.")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/gotext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"go/build"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -310,7 +309,7 @@ func help(args []string) {
if err != nil {
logf("Could not format generated docs: %v\n", err)
}
if err := ioutil.WriteFile("doc.go", b, 0666); err != nil {
if err := os.WriteFile("doc.go", b, 0666); err != nil {
logf("Could not create file alldocs.go: %v\n", err)
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions collate/maketables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
Expand Down Expand Up @@ -187,7 +186,7 @@ func failOnError(e error) {

func openArchive() *zip.Reader {
f := gen.OpenCLDRCoreZip()
buffer, err := ioutil.ReadAll(f)
buffer, err := io.ReadAll(f)
f.Close()
failOnError(err)
archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
Expand Down
3 changes: 1 addition & 2 deletions collate/reg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"flag"
"io"
"io/ioutil"
"log"
"path"
"regexp"
Expand Down Expand Up @@ -133,7 +132,7 @@ func convHex(line int, s string) int {

func loadTestData() []Test {
f := gen.OpenUnicodeFile("UCA", "", "CollationTest.zip")
buffer, err := ioutil.ReadAll(f)
buffer, err := io.ReadAll(f)
f.Close()
Error(err)
archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
Expand Down
4 changes: 2 additions & 2 deletions encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package encoding_test

import (
"io/ioutil"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestReplacement(t *testing.T) {
want = "AB\x00CD\ufffdYZ"
}
sr := strings.NewReader("AB\x00CD\x80YZ")
g, err := ioutil.ReadAll(transform.NewReader(sr, enc))
g, err := io.ReadAll(transform.NewReader(sr, enc))
if err != nil {
t.Errorf("%s: ReadAll: %v", direction, err)
continue
Expand Down
12 changes: 6 additions & 6 deletions encoding/internal/enctest/enctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -105,7 +105,7 @@ func Benchmark(b *testing.B, enc encoding.Encoding) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := transform.NewReader(bytes.NewReader(src), transformer)
io.Copy(ioutil.Discard, r)
io.Copy(io.Discard, r)
}
})
}
Expand Down Expand Up @@ -157,15 +157,15 @@ func load(direction string, enc encoding.Encoding) ([]byte, []byte, Transcoder,
dstFile, srcFile = srcFile, dstFile
coder = enc.NewDecoder()
}
dst, err := ioutil.ReadFile(dstFile)
dst, err := os.ReadFile(dstFile)
if err != nil {
if dst, err = ioutil.ReadFile("../" + dstFile); err != nil {
if dst, err = os.ReadFile("../" + dstFile); err != nil {
return nil, nil, nil, err
}
}
src, err := ioutil.ReadFile(srcFile)
src, err := os.ReadFile(srcFile)
if err != nil {
if src, err = ioutil.ReadFile("../" + srcFile); err != nil {
if src, err = os.ReadFile("../" + srcFile); err != nil {
return nil, nil, nil, err
}
}
Expand Down
4 changes: 2 additions & 2 deletions encoding/traditionalchinese/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package traditionalchinese

import (
"fmt"
"io/ioutil"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestBig5CircumflexAndMacron(t *testing.T) {
"\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
want := "ÓǑÒ\u00ca\u0304\u00ca\u030cỀÊ " +
\u00ea\u0304ế\u00ea\u030cề"
dst, err := ioutil.ReadAll(transform.NewReader(
dst, err := io.ReadAll(transform.NewReader(
strings.NewReader(src), Big5.NewDecoder()))
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions feature/plural/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package plural

import (
"fmt"
"io/ioutil"
"io"
"reflect"
"strconv"

Expand Down Expand Up @@ -56,7 +56,7 @@ type Interface interface {
func Selectf(arg int, format string, cases ...interface{}) catalog.Message {
var p parser
// Intercept the formatting parameters of format by doing a dummy print.
fmt.Fprintf(ioutil.Discard, format, &p)
fmt.Fprintf(io.Discard, format, &p)
m := &message{arg, kindDefault, 0, cases}
switch p.verb {
case 'g':
Expand Down
5 changes: 2 additions & 3 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -256,7 +255,7 @@ func copyPackage(dirSrc, dirDst, search, replace string) {
filepath.Dir(file) != dirSrc {
return nil
}
b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err != nil || bytes.Contains(b, []byte("\n// +build ignore")) {
return err
}
Expand All @@ -275,7 +274,7 @@ func copyPackage(dirSrc, dirDst, search, replace string) {
}
file = filepath.Join(dirDst, base)
vprintf("=== COPY %s\n", file)
return ioutil.WriteFile(file, b, 0666)
return os.WriteFile(file, b, 0666)
})
if err != nil {
fmt.Println("Copying exported files failed:", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/cldrtree/cldrtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package cldrtree
import (
"bytes"
"flag"
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"reflect"
"regexp"
Expand Down Expand Up @@ -303,11 +303,11 @@ func TestGen(t *testing.T) {

file := filepath.Join("testdata", tc, "output.go")
if *genOutput {
ioutil.WriteFile(file, got, 0700)
os.WriteFile(file, got, 0700)
t.SkipNow()
}

b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/gen/bitfield/bitfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package bitfield
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -222,7 +222,7 @@ func (t test1) Baz() int8 {
`

func mustRead(filename string) string {
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
Expand Down
15 changes: 7 additions & 8 deletions internal/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"go/build"
"go/format"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -175,7 +174,7 @@ func getLocalDir() string {
if err := os.MkdirAll(dir, permissions); err != nil {
log.Fatalf("Could not create directory: %v", err)
}
ioutil.WriteFile(readme, []byte(readmeTxt), permissions)
os.WriteFile(readme, []byte(readmeTxt), permissions)
}
return dir
}
Expand Down Expand Up @@ -213,15 +212,15 @@ func open(file, urlRoot, path string) io.ReadCloser {
}
r := get(urlRoot, path)
defer r.Close()
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
log.Fatalf("Could not download file: %v", err)
}
os.MkdirAll(filepath.Dir(file), permissions)
if err := ioutil.WriteFile(file, b, permissions); err != nil {
if err := os.WriteFile(file, b, permissions); err != nil {
log.Fatalf("Could not create file: %v", err)
}
return ioutil.NopCloser(bytes.NewReader(b))
return io.NopCloser(bytes.NewReader(b))
}

func get(root, path string) io.ReadCloser {
Expand Down Expand Up @@ -280,13 +279,13 @@ func fileToPattern(filename string) string {
func updateBuildTags(pattern string) {
for _, t := range tags {
oldFile := fmt.Sprintf(pattern, t.version)
b, err := ioutil.ReadFile(oldFile)
b, err := os.ReadFile(oldFile)
if err != nil {
continue
}
build := fmt.Sprintf("// +build %s", t.buildTags)
b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build))
err = ioutil.WriteFile(oldFile, b, 0644)
err = os.WriteFile(oldFile, b, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -334,7 +333,7 @@ func WriteGo(w io.Writer, pkg, tags string, b []byte) (n int, err error) {
// Repackage rewrites a Go file from belonging to package main to belonging to
// the given package.
func Repackage(inFile, outFile, pkg string) {
src, err := ioutil.ReadFile(inFile)
src, err := os.ReadFile(inFile)
if err != nil {
log.Fatalf("reading %s: %v", inFile, err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/language/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"reflect"
Expand Down Expand Up @@ -921,7 +920,7 @@ func (b *builder) writeRegion() {
r := gen.OpenIANAFile("domains/root/db")
defer r.Close()

buf, err := ioutil.ReadAll(r)
buf, err := io.ReadAll(r)
failOnError(err)
re := regexp.MustCompile(`"/domains/root/db/([a-z]{2}).html"`)
for _, m := range re.FindAllSubmatch(buf, -1) {
Expand Down
5 changes: 2 additions & 3 deletions internal/testtext/codesize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package testtext
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -26,13 +25,13 @@ import (
// See dict_test.go in the display package for an example.
func CodeSize(s string) (int, error) {
// Write the file.
tmpdir, err := ioutil.TempDir(os.TempDir(), "testtext")
tmpdir, err := os.MkdirTemp(os.TempDir(), "testtext")
if err != nil {
return 0, fmt.Errorf("testtext: failed to create tmpdir: %v", err)
}
defer os.RemoveAll(tmpdir)
filename := filepath.Join(tmpdir, "main.go")
if err := ioutil.WriteFile(filename, []byte(s), 0644); err != nil {
if err := os.WriteFile(filename, []byte(s), 0644); err != nil {
return 0, fmt.Errorf("testtext: failed to write main.go: %v", err)
}

Expand Down
5 changes: 2 additions & 3 deletions internal/triegen/example_compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package triegen_test
import (
"fmt"
"io"
"io/ioutil"

"golang.org/x/text/internal/triegen"
)
Expand All @@ -17,12 +16,12 @@ func ExampleCompacter() {
for r := rune(0); r < 10000; r += 64 {
t.Insert(r, 0x9015BADA55^uint64(r))
}
sz, _ := t.Gen(ioutil.Discard)
sz, _ := t.Gen(io.Discard)

fmt.Printf("Size normal: %5d\n", sz)

var c myCompacter
sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c))
sz, _ = t.Gen(io.Discard, triegen.Compact(&c))

fmt.Printf("Size compacted: %5d\n", sz)

Expand Down
4 changes: 2 additions & 2 deletions internal/triegen/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package triegen_test

import (
"fmt"
"io/ioutil"
"io"
"math/rand"
"unicode"

Expand All @@ -15,7 +15,7 @@ import (

const seed = 0x12345

var genWriter = ioutil.Discard
var genWriter = io.Discard

func randomRunes() map[rune]uint8 {
rnd := rand.New(rand.NewSource(seed))
Expand Down
Loading