1
0
Fork 0

added example file of writing an error struct/method combo

This commit is contained in:
Stephen M. McQuay 2012-08-27 22:41:36 -06:00
parent dbcd180706
commit 697fe25cfe
1 changed files with 31 additions and 0 deletions

31
errors/errors.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"fmt"
"time"
)
type SMBError struct {
What string
When time.Time
}
// the receiver and that which is returned from generate_error must match
// types. You may change this to an SMBError pointer recevier, or change the
// return from generate_error to be an address.
func (e SMBError) Error() string {
return fmt.Sprintf("%v at %v", e.What, e.When)
}
func generate_error() error {
return SMBError{
"Here is a silly error",
time.Now(),
}
}
func main() {
if err := generate_error(); err != nil {
fmt.Println(err)
}
}