You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
funcparseFile(pathstring, cfginterface{}) error {
// open the configuration filef, err:=os.OpenFile(path, os.O_RDONLY|os.O_SYNC, 0)
iferr!=nil {
returnerr
}
deferf.Close()
// parse the file depending on the file typeswitchext:=strings.ToLower(filepath.Ext(path)); ext {
case".yaml", ".yml":
err=ParseYAML(f, cfg)
case".json":
err=ParseJSON(f, cfg)
case".toml":
err=ParseTOML(f, cfg)
case".edn":
err=parseEDN(f, cfg)
case".env":
err=parseENV(f, cfg)
default:
returnfmt.Errorf("file format '%s' doesn't supported by the parser", ext)
}
iferr!=nil {
returnfmt.Errorf("config file parsing error: %s", err.Error())
}
returnnil
}
In this function, it would be better to return an error with the "%w" verb instead of "%s". In that case, when using cleanenv.ReadConfig, the returned error can be checked with the errors.Is(err, io.EOF) function.
Let's say I want to allow the user to leave the config file empty so that default values will be taken. But for this, I am now forced to check it with function like this:
In this function, it would be better to return an error with the "%w" verb instead of "%s". In that case, when using cleanenv.ReadConfig, the returned error can be checked with the errors.Is(err, io.EOF) function.
Let's say I want to allow the user to leave the config file empty so that default values will be taken. But for this, I am now forced to check it with function like this:
The text was updated successfully, but these errors were encountered: