I'm in the situation where I have a function which can return up to 5 different errors, but I'm not sure which is considered correct by convention in Go. I've made a small MSVC example below to illustrate my issue.
Use the if syntax to block scope my errors.
if err := validatePassword(password); err != nil {
return err
}
if passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost); err != nil {
return err
}
OR name each err differntly (seems cumbersome...)
errValidatePassword := validatePassword(password)
if errValidatePassword != nil {
return errValidatePassword
}
passwordHash, errPasswordHash := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if errPasswordHash != nil {
return errPasswordHash
}
OR use a named return.
func registerAccount(email string, password string) (err error) {
err = validatePassword(password)
if err != nil {
return err
}
// POSSIBLE ISSUE HERE as err already been inistalised????
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
}
The last example is what I've seen in most places. e.g. https://github.com/kubernetes/kubernetes/blob/master/cmd/genutils/genutils.go
The compiler will not detect that as redefinition because the expression has at least one new variable.
It is a good question to ask while coding, but there's no single perfect answer. In general, handling errors in place like method 1 or method 3 is common practice in Go.
You may refer https://blog.golang.org/error-handling-and-go if needed.
Given last example, One solution is to not not use named return variable, and to introduce scopes for each functional blocks
package main
func registerAccount(email string, password string) error {
if err := validatePassword(password); err != nil {
return err
}
var passwordHash []byte
{
x, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
passwordHash = x
}
return nil
}
Note that the if already scopes the variable if you take full advantage of its syntax.