Skip to content

Instantly share code, notes, and snippets.

@duglin
Created May 15, 2015 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duglin/689b041823602de05e00 to your computer and use it in GitHub Desktop.
Save duglin/689b041823602de05e00 to your computer and use it in GitHub Desktop.
error thought
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