added example file of writing an error struct/method combo
This commit is contained in:
parent
dbcd180706
commit
697fe25cfe
31
errors/errors.go
Normal file
31
errors/errors.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user