I would like to define my Error Codes
in a package models
.
error.go
package models
const{
EOK = iota
EFAILED
}
How can I use them in another package without referring to them as models.EOK
. I would like to use directly as EOK, since these codes would be common across all packages.
Is it the right way to do it? Any better alternatives?
You can use the dot
import syntax to import the exported symbols from another package directly into your package's namespace (godoc):
import . "models"
This way you could directly refer to the EOK
constant without prefixing it with models
.
However I'd strongly advice against doing so, as it generates rather unreadable code. see below
models
. This is considered bad style as it will easily globber. Even for small projects, that are used only internally, use something like myname/models
. see goblog
error
values, e.g. errors.New
(godoc) and fmt.Errorf
(godoc).
For a general introduction on go and error handling see goblog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With