xxxxxxxxxx
package main
import (
"fmt"
"io/fs"
"os"
)
func main() {
data, _ := fs.ReadFile(os.DirFS("."), "index.txt")
fmt.Println(string(data))
}
xxxxxxxxxx
file_data, err := ioutil.ReadFile("./index.html")
if err != nil {
fmt.Println(err)
}
fmt.Println(file_data)
xxxxxxxxxx
dat, err := ioutil.ReadFile("/tmp/dat")
check(err)
fmt.Print(string(dat))
xxxxxxxxxx
Instead of,❌ioutil.ReadFile use ✔ os.ReadFile
https://pkg.go.dev/io/ioutil#ReadFile
ioutil.ReadFile is deprecated: As of Go 1.16, this function simply calls [os.ReadFile].
Deprecated code:
file_data, err := ioutil.ReadFile("./index.html")
if err != nil {
fmt.Println(err)
}
fmt.Println(file_data)
Corrected code:
file_data, err := os.ReadFile("./index.html")
if err != nil {
fmt.Println(err)
}
fmt.Println(file_data)