Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active August 6, 2021 11:37
Show Gist options
  • Save percybolmer/9a2c106fd131dee41efb65651e804e00 to your computer and use it in GitHub Desktop.
Save percybolmer/9a2c106fd131dee41efb65651e804e00 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)
}
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