Hi new at go, here. I’d like some input on a super simple go function that reads a user’s config file and exits 1 if there’s some kind of problem opening/reading it. Eventually I will obviously parse the JSON, but being new to go, wanted to start off simple and see if I am on the right track idiomatically. Is this idiomatic, in that the programmer has to keep checking for errors as they go? I don’t like err2 variable name. I generally wouldn’t use pointers unless I needed to, and would rather return a string. Is this the idiomatic way to return an optional string? It seems unduly verbose to me.
Any comments are welcome.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
)
// Attempts to read the .sinkerrc.json file in the user's
// home directory as a pointer to a string.
func readSinkerRc() (*string, error) {
homdir, err := os.UserHomeDir()
if err != nil {
// problem getting homedir
return nil, err
}
dat, err2 := ioutil.ReadFile(path.Join(homdir, ".sinkerrc.json"))
if err2 != nil {
// Problem reading file.
return nil, err2
}
ret := string(dat)
// return a pointer to the data so we could also return nil for error case.
return &ret, err
}
func main() {
dat, err := readSinkerRc()
if err != nil {
log.Fatal("Problem reading your .sinkerrc.json file: " + err.Error())
}
fmt.Println(*dat)
}