Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 6, 2021 11:37
Show Gist options
  • Save percybolmer/bb8c6d3ef6427b9d817c3a6aec82af3b to your computer and use it in GitHub Desktop.
Save percybolmer/bb8c6d3ef6427b9d817c3a6aec82af3b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"errors"
)
type DateError struct {
Message string
Date time.Time
}
// This part is updated so that we declare that the method receiver is only Pointers to DateError
func (de DateError) Error() string {
return fmt.Sprintf("%s: %s", de.Date.String(), de.Message)
}
// NewError now returns an Pointer
func NewError(message string) *DateError {
return &DateError{
Message:message,
Date: time.Now(),
}
}
// PrintError can take in an DateError since DateError fulfills the error interface
func PrintError(err error) {
fmt.Println(err.Error())
}
func main() {
de := NewError("Auch, I failed")
regularErr := errors.New("Another error")
PrintError(de)
PrintError(regularErr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment