Use the os
-Package and not the io/ioutil
because since the go version 1.16 all the functions are also on the os
-Package.
import "os"
var filename string = "./file.txt"
file, err := os.ReadFile(filename)
if err != nil {
fmt.Println("can not get file with name: " + filename)
return
}
fileContent := ""
file, err := os.Open("file.txt")
if err != nil {
fileContent = fmt.Sprintf("error during to open the file 'file.txt' -> %s", err.Error())
}
defer file.Close()
content, err := io.ReadAll(file)
if err != nil {
fileContent = fmt.Sprintf("error during to open the file 'file.txt'")
}
fileContent = string(content)