all: replace io/ioutil with io and os package

Signed-off-by: cui fliter <imcusg@gmail.com>
This commit is contained in:
cui fliter 2022-09-14 11:55:25 +08:00
Родитель b0ca10ff35
Коммит ad314caaff
21 изменённых файлов: 56 добавлений и 69 удалений

Просмотреть файл

@ -16,7 +16,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"reflect"
"strconv"
@ -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.")
}

Просмотреть файл

@ -16,7 +16,6 @@ import (
"go/build"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"strings"
@ -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 {

Просмотреть файл

@ -17,7 +17,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
@ -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)))

Просмотреть файл

@ -10,7 +10,6 @@ import (
"bytes"
"flag"
"io"
"io/ioutil"
"log"
"path"
"regexp"
@ -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)))

Просмотреть файл

@ -5,7 +5,7 @@
package encoding_test
import (
"io/ioutil"
"io"
"strings"
"testing"
@ -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

Просмотреть файл

@ -8,7 +8,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
@ -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)
}
})
}
@ -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
}
}

Просмотреть файл

@ -6,7 +6,7 @@ package traditionalchinese
import (
"fmt"
"io/ioutil"
"io"
"strings"
"testing"
@ -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)

Просмотреть файл

@ -6,7 +6,7 @@ package plural
import (
"fmt"
"io/ioutil"
"io"
"reflect"
"strconv"
@ -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':

5
gen.go
Просмотреть файл

@ -14,7 +14,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"path"
@ -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
}
@ -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)

Просмотреть файл

@ -7,9 +7,9 @@ package cldrtree
import (
"bytes"
"flag"
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"reflect"
"regexp"
@ -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)
}

Просмотреть файл

@ -7,7 +7,7 @@ package bitfield
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
)
@ -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)
}

Просмотреть файл

@ -25,7 +25,6 @@ import (
"go/build"
"go/format"
"io"
"io/ioutil"
"log"
"net/http"
"os"
@ -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
}
@ -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 {
@ -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)
}
@ -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)
}

Просмотреть файл

@ -15,7 +15,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"reflect"
@ -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) {

Просмотреть файл

@ -7,7 +7,6 @@ package testtext
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -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)
}

Просмотреть файл

@ -7,7 +7,6 @@ package triegen_test
import (
"fmt"
"io"
"io/ioutil"
"golang.org/x/text/internal/triegen"
)
@ -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)

Просмотреть файл

@ -6,7 +6,7 @@ package triegen_test
import (
"fmt"
"io/ioutil"
"io"
"math/rand"
"unicode"
@ -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))

Просмотреть файл

@ -11,7 +11,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path"
@ -40,7 +39,7 @@ func TestFullCycle(t *testing.T) {
t.Skipf("skipping because 'go' command is unavailable: %v", err)
}
GOPATH, err := ioutil.TempDir("", "pipeline_test")
GOPATH, err := os.MkdirTemp("", "pipeline_test")
if err != nil {
t.Fatal(err)
}
@ -66,7 +65,7 @@ func TestFullCycle(t *testing.T) {
wd, _ := os.Getwd()
defer os.Chdir(wd)
dirs, err := ioutil.ReadDir(testdata)
dirs, err := os.ReadDir(testdata)
if err != nil {
t.Fatal(err)
}
@ -124,11 +123,11 @@ func copyTestdata(t *testing.T, dst string) {
return os.MkdirAll(filepath.Join(dst, rel), 0755)
}
data, err := ioutil.ReadFile(p)
data, err := os.ReadFile(p)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dst, rel), data, 0644)
return os.WriteFile(filepath.Join(dst, rel), data, 0644)
})
if err != nil {
t.Fatal(err)
@ -145,17 +144,17 @@ func initTestdataModule(t *testing.T, dst string) {
replace golang.org/x/text => %s
`, xTextDir)
if err := ioutil.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil {
if err := os.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil {
t.Fatal(err)
}
// Copy in the checksums from the parent module so that we won't
// need to re-fetch them from the checksum database.
data, err := ioutil.ReadFile(filepath.Join(xTextDir, "go.sum"))
data, err := os.ReadFile(filepath.Join(xTextDir, "go.sum"))
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil {
if err := os.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil {
t.Fatal(err)
}
@ -194,17 +193,17 @@ func checkOutput(t *testing.T, gen string, testdataDir string) {
return nil
}
got, err := ioutil.ReadFile(gotFile)
got, err := os.ReadFile(gotFile)
if err != nil {
t.Errorf("failed to read %q", gotFile)
return nil
}
if *genFiles {
if err := ioutil.WriteFile(wantFile, got, 0644); err != nil {
if err := os.WriteFile(wantFile, got, 0644); err != nil {
t.Fatal(err)
}
}
want, err := ioutil.ReadFile(wantFile)
want, err := os.ReadFile(wantFile)
if err != nil {
t.Errorf("failed to read %q", wantFile)
} else {
@ -242,7 +241,7 @@ func writeJSON(t *testing.T, path string, x interface{}) {
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path, data, 0644); err != nil {
if err := os.WriteFile(path, data, 0644); err != nil {
t.Fatal(err)
}
}

Просмотреть файл

@ -8,7 +8,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"strconv"
"strings"
"testing"
@ -648,7 +648,7 @@ func TestReader(t *testing.T) {
// exported API. We override them manually.
r.dst = make([]byte, tc.dstSize)
r.src = make([]byte, tc.srcSize)
got, err := ioutil.ReadAll(r)
got, err := io.ReadAll(r)
str := string(got)
if str != tc.wantStr || err != tc.wantErr {
t.Errorf("\ngot %q, %v\nwant %q, %v", str, err, tc.wantStr, tc.wantErr)

Просмотреть файл

@ -10,7 +10,6 @@ import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -160,7 +159,7 @@ func (zl zipLoader) Reader(i int) (io.ReadCloser, error) {
// DecodeZip loads CLDR data from the zip archive for which r is the source.
func (d *Decoder) DecodeZip(r io.Reader) (cldr *CLDR, err error) {
buffer, err := ioutil.ReadAll(r)
buffer, err := io.ReadAll(r)
if err != nil {
return nil, err
}

Просмотреть файл

@ -15,7 +15,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
@ -30,7 +29,7 @@ func main() {
flag.Parse()
r := gen.OpenCLDRCoreZip()
buffer, err := ioutil.ReadAll(r)
buffer, err := io.ReadAll(r)
if err != nil {
log.Fatal("Could not read zip file")
}

Просмотреть файл

@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -927,14 +926,14 @@ func TestString(t *testing.T) {
func runNM(code string) (string, error) {
// Write the file.
tmpdir, err := ioutil.TempDir(os.TempDir(), "normalize_test")
tmpdir, err := os.MkdirTemp(os.TempDir(), "normalize_test")
if err != nil {
return "", fmt.Errorf("failed to create tmpdir: %v", err)
}
defer os.RemoveAll(tmpdir)
goTool := filepath.Join(runtime.GOROOT(), "bin", "go")
filename := filepath.Join(tmpdir, "main.go")
if err := ioutil.WriteFile(filename, []byte(code), 0644); err != nil {
if err := os.WriteFile(filename, []byte(code), 0644); err != nil {
return "", fmt.Errorf("failed to write main.go: %v", err)
}
outputFile := filepath.Join(tmpdir, "main")