Created
May 15, 2015 18:53
-
-
Save duglin/689b041823602de05e00 to your computer and use it in GitHub Desktop.
error thought
This file contains 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" | |
) | |
type ErrorCode struct { | |
code int | |
next *ErrorCode | |
} | |
var ( | |
ErrorBadType ErrorCode = ErrorCode{1, nil} | |
) | |
func (ec *ErrorCode) String() string { | |
res := "{" | |
for ; ec != nil; ec = ec.next { | |
if len(res) > 1 { | |
res += ", " | |
} | |
res += fmt.Sprintf("[ %d ]", ec.code) | |
} | |
res = res + " }" | |
return res | |
} | |
func (ec *ErrorCode) IsA(c ErrorCode) bool { | |
return ec.Contains(c) | |
} | |
func (ec *ErrorCode) Contains(c ErrorCode) bool { | |
for ; ec != nil; ec = ec.next { | |
if ec.code == c.code { | |
return true | |
} | |
} | |
return false | |
} | |
func (ec *ErrorCode) Add(c ErrorCode) *ErrorCode { | |
top := ec | |
if top == nil { | |
return &ErrorCode{c.code, c.next} | |
} | |
for ; ec.next != nil; ec = ec.next { | |
} | |
ec.next = &ErrorCode{c.code, c.next} | |
return top | |
} | |
func foo() ErrorCode { | |
return ErrorBadType | |
} | |
func main() { | |
e := foo() | |
fmt.Printf("e: %q\n", e.String()) | |
e.Add(foo()) | |
fmt.Printf("e: %q\n", e.String()) | |
fmt.Printf("isA: %q\n", e.IsA(ErrorBadType)) | |
fmt.Printf("==: %q\n", e == ErrorBadType) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment