News for hackers. Stuff that matters.
Re:The Reliability of Go by frou_dh on Sunday October 20, 2013 @06:46PM (#6581887)
> The PRIMARY reason Go is so reliable is also a reason many people hate go: you _have_ to handle every error. There are no exceptions, you know if a line of code can fail or not, and you have to handle it.

> Pedantics: yes, you can assign errors to "_" variables to ignore them, but this is very actively discouraged in Go and it is one of those you're-doing-it-wrong kind of things

The _ thing is fine because it's visible, but what's potentially insidious is the compiler having no complaints about:

    func DoIt() error {
        return errors.New("No")
    }

    func main() {
        DoIt()
    }
I do like Go a lot, but this is a valid asterisk on talk of the safety of its approach to errors.

---

For non-gophers: The reason this doesn't come up all the time is that functions often return multiple values, at least one of which you actively need, at which point you are forced to do something with any error that comes along for the ride.

Re:The Reliability of Go by mitchellh on Sunday October 20, 2013 @06:48PM (#6581894)
True, but I consider this the same idea. If you look up the API of a method and see an "error" return type, it is your responsibility to handle it, and you're very encouraged to do so.
Re:The Reliability of Go by twotwotwo on Sunday October 20, 2013 @07:24PM (#6582039)
I'd love it if "go vet" or something complained about implicitly discarded errors. If you mean to ignore an error, you can always assign to _.
Re:The Reliability of Go by twotwotwo on Sunday October 20, 2013 @10:32PM (#6582880)
Below, mseepgood pointed out https://github.com/kisielk/errcheck exists--looks excellent.

(Still would be cool if the Gophers put something like it in the std distro.)

Re:The Reliability of Go by pcwalton on Sunday October 20, 2013 @07:21PM (#6582024)
I'm confused as to what you mean by "the compiler forces you to handle errors", then. Do you just mean that errors are documented?
Re:The Reliability of Go by hornetblack on Sunday October 20, 2013 @08:37PM (#6582348)
If I attempt to grab the result of a function eg:

    f := os.Create(fname)
    defer(f.Close())
It won't compile as `os.Create` returns `(file, error)`. So you must either:

    // Proper error handling
    f, err := os.Create(fname)
    if err != nil {
        log.Fatalf("Could not create '%s'", fname)
        // Well may want to do something other than crash.
    }
    defer(f.close())
    
If you ignore the error it's obvious

    f, _ := os.Create(fname)
    defer(f.Close())
It's like doing this in Java

    File f;
    try {
        f = new File(fname);
    } catch (Exception e) {
        // Ignore Exception
    }
Re:The Reliability of Go by drbawb on Monday October 21, 2013 @03:51PM (#6587787)
Possibly worth pointing out to non-gophers: in `Go` it is illegal to declare an unused variable.

So if you write `f, err := os.Create("/bad/path");`, and that is the first time `err` has been declared & used in that scope, you can't just let `err` go unused or your program won't compile.

Re:The Reliability of Go by elithrar on Sunday October 20, 2013 @07:49PM (#6582131)
The quotes don't make any sense: mitchellh didn't say that at all.
Re:The Reliability of Go by Locke1689 on Sunday October 20, 2013 @08:05PM (#6582195)
Eh. He said,

The PRIMARY reason Go is so reliable is also a reason many people hate go: you _have_ to handle every error.

If the compiler isn't enforcing it, you don't have to do anything. At best you can say the language suggests it, but if there isn't even a warning I don't see how that's different from pretty much every typed language.

Re:The Reliability of Go by drbawb on Monday October 21, 2013 @04:03PM (#6587829)
>isn't even a warning

This is actually enforced [in some cases] as a compile-time error. (BTW the `gc` family of Go compilers don't have warnings.)

The two errors in particular that help trap this are:

1. All declared variables must be used.

2. All return values must be assigned to a variable. (Go allows for multiple return values from a function.)

---

Idiomatically one would store [one or more] return values from a function with a short-form declaration (`:=`) which declares and assigns the LHS to the RHS.

`a,err := someFunc()` where `someFunc()` returns `(SomeType, error)` declares and assigns `a int` and `err error`.

If you declare `a,err := someFunc()` and do not use `a` or `err` elsewhere in scope, your program simply won't compile.

The only way to squelch that error is to either use the variables OR you can use a `_` on the LHS. (Which makes an "assignment" to a blank identifier, e.g: discards the value.)

---

An example of where this _wouldn't_ be enforced by the compiler is if you've declared [and used] `err` elsewhere in the scope and you are _reusing_ the identifier.

For e.g: if you declared, assigned, used, and reassigned `err`. The compiler won't force you to use it _after_ the second assignment, so that second assignment could go untrapped with no complains from the compiler.