In most of my code I create a function to handle errors. looks something like this:
func errorHandle(err error, str string, ex bool) {
if err != nil {
fmt.Printf("error: %s -- %v\n",str, err)
}
if ex {
os.Exit(1)
}
}
This cleans up my code:
inFile, err := os.ReadFile("Mydata.txt")
errorHandle(err,"read mydata.txt",true) // The true will exit
So you see that I can exit the program when there is an error, what I want to know is if it's possible to do the same thing inside a function, to where I don't exit the program but return from the function:
func OpenFile( filename string) []byte {
inFile,err := os.ReadFile(filename)
errReturn(err, "read "+filname,true)
When there is an error it would return from the OpenFile function,