all: replace io/ioutil with io and os

Closes #1770
This commit is contained in:
Hajime Hoshi 2022-09-15 02:45:36 +09:00
parent d0556af8a4
commit 5404e4d68a
12 changed files with 25 additions and 45 deletions

View File

@ -16,7 +16,6 @@ package audio
import ( import (
"io" "io"
"io/ioutil"
"sync" "sync"
) )
@ -64,7 +63,7 @@ func (p *dummyPlayer) Play() {
p.playing = true p.playing = true
p.m.Unlock() p.m.Unlock()
go func() { go func() {
if _, err := ioutil.ReadAll(p.r); err != nil { if _, err := io.ReadAll(p.r); err != nil {
panic(err) panic(err)
} }
p.m.Lock() p.m.Lock()

View File

@ -16,7 +16,7 @@ package convert_test
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"math" "math"
"testing" "testing"
@ -70,7 +70,7 @@ func TestResampling(t *testing.T) {
for _, c := range cases { for _, c := range cases {
inB := newSoundBytes(c.In) inB := newSoundBytes(c.In)
outS := convert.NewResampling(bytes.NewReader(inB), int64(len(inB)), c.In, c.Out) outS := convert.NewResampling(bytes.NewReader(inB), int64(len(inB)), c.In, c.Out)
gotB, err := ioutil.ReadAll(outS) gotB, err := io.ReadAll(outS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -21,7 +21,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -82,7 +81,7 @@ func invokeOriginalGobind(lang string) (pkgName string, err error) {
func run() error { func run() error {
writeFile := func(filename string, content string) error { writeFile := func(filename string, content string) error {
if err := ioutil.WriteFile(filepath.Join(*outdir, filename), []byte(content), 0644); err != nil { if err := os.WriteFile(filepath.Join(*outdir, filename), []byte(content), 0644); err != nil {
return err return err
} }
return nil return nil

View File

@ -17,7 +17,6 @@ package main
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -79,7 +78,7 @@ func exe(filename string) string {
} }
func prepareGomobileCommands() (string, error) { func prepareGomobileCommands() (string, error) {
tmp, err := ioutil.TempDir("", "ebitenmobile-") tmp, err := os.MkdirTemp("", "ebitenmobile-")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -121,7 +120,7 @@ func prepareGomobileCommands() (string, error) {
if err := runGo("mod", "init", modname); err != nil { if err := runGo("mod", "init", modname); err != nil {
return tmp, err return tmp, err
} }
if err := ioutil.WriteFile("tools.go", []byte(fmt.Sprintf(`%s if err := os.WriteFile("tools.go", []byte(fmt.Sprintf(`%s
package %s package %s
@ -160,7 +159,7 @@ import (
if err := os.Mkdir("src", 0755); err != nil { if err := os.Mkdir("src", 0755); err != nil {
return tmp, err return tmp, err
} }
if err := ioutil.WriteFile(filepath.Join("src", "gobind.go"), gobind_go, 0644); err != nil { if err := os.WriteFile(filepath.Join("src", "gobind.go"), gobind_go, 0644); err != nil {
return tmp, err return tmp, err
} }

View File

@ -23,7 +23,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -246,13 +245,13 @@ func doBind(args []string, flagset *flag.FlagSet, buildOS string) error {
frameworkNameBase = strings.Title(frameworkNameBase) frameworkNameBase = strings.Title(frameworkNameBase)
dir := filepath.Join(buildO, name, frameworkNameBase+".framework", "Versions", "A") dir := filepath.Join(buildO, name, frameworkNameBase+".framework", "Versions", "A")
if err := ioutil.WriteFile(filepath.Join(dir, "Headers", prefixUpper+"EbitenViewController.h"), []byte(replacePrefixes(objcH)), 0644); err != nil { if err := os.WriteFile(filepath.Join(dir, "Headers", prefixUpper+"EbitenViewController.h"), []byte(replacePrefixes(objcH)), 0644); err != nil {
return err return err
} }
// TODO: Remove 'Ebitenmobileview.objc.h' here. Now it is hard since there is a header file importing // TODO: Remove 'Ebitenmobileview.objc.h' here. Now it is hard since there is a header file importing
// that header file. // that header file.
fs, err := ioutil.ReadDir(filepath.Join(dir, "Headers")) fs, err := os.ReadDir(filepath.Join(dir, "Headers"))
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,7 +16,7 @@ package ebitenutil
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"net/http" "net/http"
) )
@ -40,7 +40,7 @@ func OpenFile(path string) (ReadSeekCloser, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -27,7 +27,6 @@ import (
"image/color" "image/color"
_ "image/png" _ "image/png"
"io" "io"
"io/ioutil"
"log" "log"
"time" "time"
@ -179,7 +178,7 @@ func NewPlayer(game *Game, audioContext *audio.Context, musicType musicType) (*P
log.Fatal(err) log.Fatal(err)
return return
} }
b, err := ioutil.ReadAll(s) b, err := io.ReadAll(s)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return

View File

@ -19,11 +19,9 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -46,13 +44,7 @@ var (
) )
func init() { func init() {
f, err := os.Open(filepath.Join("..", "..", "resources", "fonts", "pressstart2p.ttf")) b, err := os.ReadFile(filepath.Join("..", "..", "resources", "fonts", "pressstart2p.ttf"))
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -19,7 +19,7 @@ package gamepad
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -77,7 +77,7 @@ func (g *nativeGamepadsImpl) init(gamepads *gamepads) error {
g.watch = watch g.watch = watch
} }
ents, err := ioutil.ReadDir(dirName) ents, err := os.ReadDir(dirName)
if err != nil { if err != nil {
return fmt.Errorf("gamepad: ReadDir(%s) failed: %w", dirName, err) return fmt.Errorf("gamepad: ReadDir(%s) failed: %w", dirName, err)
} }

View File

@ -23,7 +23,6 @@ import (
"go/format" "go/format"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -72,12 +71,6 @@ func run() error {
} }
for _, f := range files { for _, f := range files {
in, err := os.Open(filepath.Join(dir, f))
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(prefix + f) out, err := os.Create(prefix + f)
if err != nil { if err != nil {
return err return err
@ -86,7 +79,7 @@ func run() error {
// TODO: Remove call of RegisterDecoder // TODO: Remove call of RegisterDecoder
data, err := ioutil.ReadAll(in) data, err := os.ReadFile(filepath.Join(dir, f))
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,7 +19,7 @@ package processtest_test
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
@ -28,7 +28,7 @@ import (
func TestPrograms(t *testing.T) { func TestPrograms(t *testing.T) {
dir := "testdata" dir := "testdata"
ents, err := ioutil.ReadDir(dir) ents, err := os.ReadDir(dir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -18,7 +18,7 @@ import (
"fmt" "fmt"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -86,7 +86,7 @@ func TestCompile(t *testing.T) {
t.Skip("file open might not be implemented in this environment") t.Skip("file open might not be implemented in this environment")
} }
files, err := ioutil.ReadDir("testdata") files, err := os.ReadDir("testdata")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -114,7 +114,7 @@ func TestCompile(t *testing.T) {
continue continue
} }
src, err := ioutil.ReadFile(filepath.Join("testdata", n)) src, err := os.ReadFile(filepath.Join("testdata", n))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -127,7 +127,7 @@ func TestCompile(t *testing.T) {
vsn := name + ".expected.vs" vsn := name + ".expected.vs"
if _, ok := fnames[vsn]; ok { if _, ok := fnames[vsn]; ok {
vs, err := ioutil.ReadFile(filepath.Join("testdata", vsn)) vs, err := os.ReadFile(filepath.Join("testdata", vsn))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -136,7 +136,7 @@ func TestCompile(t *testing.T) {
fsn := name + ".expected.fs" fsn := name + ".expected.fs"
if _, ok := fnames[fsn]; ok { if _, ok := fnames[fsn]; ok {
fs, err := ioutil.ReadFile(filepath.Join("testdata", fsn)) fs, err := os.ReadFile(filepath.Join("testdata", fsn))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -149,7 +149,7 @@ func TestCompile(t *testing.T) {
hlsln := name + ".expected.hlsl" hlsln := name + ".expected.hlsl"
if _, ok := fnames[hlsln]; ok { if _, ok := fnames[hlsln]; ok {
hlsl, err := ioutil.ReadFile(filepath.Join("testdata", hlsln)) hlsl, err := os.ReadFile(filepath.Join("testdata", hlsln))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -158,7 +158,7 @@ func TestCompile(t *testing.T) {
metaln := name + ".expected.metal" metaln := name + ".expected.metal"
if _, ok := fnames[metaln]; ok { if _, ok := fnames[metaln]; ok {
metal, err := ioutil.ReadFile(filepath.Join("testdata", metaln)) metal, err := os.ReadFile(filepath.Join("testdata", metaln))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }