Skip to content

Instantly share code, notes, and snippets.

@guoxingx
Created February 26, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoxingx/d170c364e6375f2191a8c433b54c04cf to your computer and use it in GitHub Desktop.
Save guoxingx/d170c364e6375f2191a8c433b54c04cf to your computer and use it in GitHub Desktop.
error with code and message
package gerror
import (
"fmt"
)
// Error with an code in int type
type Error struct {
code int
message string
}
// NewError returns an Error object
func NewError(c int, m string) Error {
return Error{c, m}
}
// Code returns the code of error
func (e Error) Code() int {
return e.code
}
// Message returns the message of error
func (e Error) Message() string {
return e.message
}
// Error implement Error() of error interface
func (e Error) Error() string {
return fmt.Sprintf("code: %d, message: %s", e.code, e.message)
}
// Same returns true if error has the same code with another
func (e Error) Same(anoter Error) bool {
return e.code == anoter.code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment