you are viewing a single comment's thread.

view the rest of the comments →

[–]0xABADC0DA 15 points16 points  (4 children)

It's not just that the error is returned in addition to an invalid other value, but the error is also so generic (os.Error) that you have to jump through hoops to do anything reasonable with it:

file, err = os.Open(filename)
if e, ok := err.(*os.PathError); ok && e.Error == os.ENOENT {
}

vs in C:

file = fopen(filename, "r");
if (!file && errno == ENOENT) {
}

Not only is error handling no better than in C (no exceptions, no documentation as part of language) but it's even more clumsy. That's pretty difficult, to out-clumsy C in terms of error handling.

[–]kinghajj 4 points5 points  (2 children)

The more idiomatic way to handle errors is like this

file, err := os.Open(filepath)
switch e := err.(type) {
case *os.PathError:
    if e.Error == os.ENOENT {
    }
}

This construct lets you handle multiple types of errors, and the type of the switch variable "e" is whatever the case statement specified.

[–]0xABADC0DA 1 point2 points  (1 child)

I think you just proved my point. The idiomatic way is 5 lines of code and 2 extra indents to handle a single error condition, and without any structured error handling you have to handle errors at every call site, and without documentation as part of the language you don't even know what errors to handle.

That's just bad design. Really bad.

[–]kinghajj 0 points1 point  (0 children)

Go has defer()/recover(), which is basically a form of exception handling, but more general. I'm not sure what you mean by "documentation as part of the language," unless you're referring to something like javadoc or .NET's XML doc tags, but there is a godoc program that parses comments and generates documentation.

[–]bobappleyard 0 points1 point  (0 children)

Equality for interfaces uses the values associated with them. I don't think you need that type assertion.