Example to create custom error

package main
import (
    "errors"
    "fmt"
    "time"
)
type CustomError struct {
    message   string
    function  string
    timestamp time.Time
}
func (e *CustomError) Error() string {
    return e.message
}
func (e *CustomError) GetHost() time.Time {
    return e.timestamp
}
func main() {
    errs := []interface{}{
        CustomError{"Custom error.", "host01.local", time.Now()},        
        errors.New("Generic error"),
    }
    for _, e := range errs {
        if v, ok := e.(CustomError); ok {
            fmt.Println("Error: ", v.Error())
            fmt.Println("  -> This error is of type CustomError")        
        } else {
            fmt.Println("Error: ", v.Error())
            fmt.Println("  -> This error is generic")
        }
    }
}
C:\golang\codes>go run example.go
Error: Custom error.
-> This error is of type CustomError
Error:
-> This error is generic

C:\golang\codes>

Thanks for reading. Please leave your valuable feedback and comments.

* Required Fields

No questions yet. Be the first to ask the question!