Function Options: return err to handle errors?

I found this git repo with go patterns:

For example this, does not allow that evaluating the given argument results in an error:

func UID(userID int) Option {
	return func(args *Options) {
		args.UID = userID
	}
}

Wouldn’t it be better, if Options would be defined like this?

type Option func(*Options) error

Hi @guettli,

In theory, setting an option should be simple enough to be an error-free operation. If the option needs to be calculated beforehand for some reason, and if this calculation can result in an error, this can be done before calling the option function.

1 Like