golang custom error
package main
import (
"fmt"
)
type MyError interface {
GetCode() int32
GetMessage() string
Error() string
}
type CustomError struct {
Code int32
Msg string
}
func (e CustomError) Error() string {
return fmt.Sprintf("code: %d, msg: %s", e.Code, e.Msg)
}
func (e CustomError) GetCode() int32 {
return e.Code
}
func (e CustomError) GetMessage() string {
return e.Msg
}
func getError(code int32, msg string) MyError {
return CustomError{Code: code, Msg: msg}
}
func main() {
var err MyError
err = getError(1234, "ou ooops")
// err = err.(MyError)
fmt.Println(err.GetCode())
fmt.Println(err.GetMessage())
fmt.Println(err.Error())
fmt.Println("Hello, playground")
}