The default structure for error messages in golang contains a string but I want to add dynamic response code and the time the error occurred to it. Any suggestions on how to do it?
error
in not a struct, is an interface.
type error interface {
Error() string
}
you can define your own error struct, just implement the Error() string
function.
type ErrorA struct {
// any field you want
}
func (e ErrorA) Error() string {
// implement this function
}
then ErrorA
can be used as error
.
ref:
1. https://golang.org/ref/spec#Errors
2. https://golang.org/ref/spec#Interface_types