Last active
August 6, 2021 11:37
-
-
Save percybolmer/bb8c6d3ef6427b9d817c3a6aec82af3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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