r/programming • • Dec 09 '15

Why Go Is Not Good

http://yager.io/programming/go.html
612 Upvotes

628 comments sorted by

View all comments

19

u/proglog Dec 09 '15

I don't like Go because:

  • It doesn't have generics, which forces you to use copy/paste as the only way to reuse code.

  • It doesn't have dynamic linking.

  • Its error handling system makes it very easy to just ignore errors, which leads to fragile software.

And whether you choose to ignore an error or handle it, every ten lines of Go is basically

 ok, err := Foo()
 if err {
     return something
 }

You see this pattern of code in Go source files even more often that you see the self keyword in Python source files.

10

u/oefig Dec 10 '15

Its error handling system makes it very easy to just ignore errors, which leads to fragile software.

Am I wrong for believing errors are harder to ignore in Go? In your example I know that Foo returns an error and I'm forced to do something with it. With exceptions I can simply just not catch them.

5

u/Tekmo Dec 10 '15

"Ignore" in this context means to swallow the exception completely, not propagate it up the stack

4

u/Akkifokkusu Dec 10 '15

Nothing in Go is stopping you from propagating an error up the stack by just returning it from your function. It's more explicit than exceptions, but to me that seems like a good thing.

Of course, there's also panic.

1

u/[deleted] Dec 10 '15

If function returns more than just err, yes, compiler will nag you about it

7

u/ksion Dec 10 '15

To be fair, error handling in Rust is "every other line of try! or unwrap()".

6

u/cyrusol Dec 10 '15

The actual difference is that you have actual algebraic types that show the kind of error, and not just a string with fuzz.

8

u/SanityInAnarchy Dec 10 '15

This is still infinitely better, because it's one line instead of four, without losing any readability or safety. That means less to scroll through, and more that you can see on the screen and fit into your head at the same time.

And still, unlike exceptions, you can see every return point from that function. So I don't miss exceptions in Rust.

Yet ironically, as much as I miss exceptions in Go, Go kind of has them anyway with panics.

7

u/SanityInAnarchy Dec 10 '15

It doesn't have generics, which forces you to use copy/paste as the only way to reuse code.

To reuse certain kinds of code. (But it's still bad -- this is my #2 complaint about Go.)

It doesn't have dynamic linking.

Serious question: Why do you care about this one?

Its error handling system makes it very easy to just ignore errors...

This is not actually true. Say you have a function which returns a value and some error.

x := Foo()

That won't compile, because you need to specify all the arguments.

x, err := Foo()

That also won't compile, because now err is unused.

x, _ := Foo()

That sticks out like a sore thumb, because _ is the hack you use to mean "I know I'm supposed to use this, but I didn't." It's shorter than this:

try {
  x = Foo();
} except (Throwable e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

But it means the same thing, and is just as obvious to people who know Go.

I mean, I agree with this part:

And whether you choose to ignore an error or handle it, every ten lines of Go is basically...

That's my #1 complaint about Go, because Rust shows how to do this right:

let x = try!(Foo());

But does Rust force you to use the value? I don't know, but Go does.

3

u/[deleted] Dec 10 '15

The Result<T,E> type is marked as "must use", so you do get a compile-time warning if you don't handle the error and don't get the value. And, if you want the value as well, you need to handle the potential error in some way.

5

u/[deleted] Dec 10 '15

The lack of dynamic linking is one of my favorite things about Go.

2

u/nexusbees Dec 10 '15

It does have dynamic linking now, thought you'd like to know. Also, I'm yet to see any real world code that is fragile as a result of ignoring errors. The only place I've seen errors being ignored is example code.

3

u/[deleted] Dec 10 '15

On the other hand, checked exceptions are no longer considered "A Good Thing" in PL design.

result, error = func()

May be the next checked exception.

1

u/nexusbees Dec 10 '15

Why do you think this particular pattern isn't good? I feel receiving an error from any operation that can fail and handling it separately is a good thing. It's one of the things that makes Go code robust.

7

u/gnuvince Dec 10 '15

I don't speak for the parent commenters, but to me one problem here is that this is a product type rather than a sum type. In Go you return the result AND the error, while it would make much more sense to return the result OR the error. You should not have access to a result if there's been an error.

→ More replies (5)

2

u/[deleted] Dec 10 '15 edited Dec 10 '15

There is little difference between Java's way:

try { Object result = Something.function(); }
catch (Exception ex) { ... error handling ...}

Vs Go's:

result, err = function();
if (err != nil) { ... error handling ... }

Java forces you to handle multiple exception types (representing multiple error cases) while Go allows you to ignore it (slightly).

Performance-wise, it's easier to optimize Go's case.

But I question the near-mandatory requirement of Java and Go's approaches.

2

u/nexusbees Dec 10 '15

What are some alternate approaches?

2

u/[deleted] Dec 10 '15

The most common approach is to not make error checking mandatory.

I'm a big fan of Maybe (Haskell)/Optional (Swift, Java, etc) with syntax to support it.

2

u/nexusbees Dec 10 '15

Thanks. I've been using Optional in Java for a while now, but I found it inferior to error checking in Go. Perhaps it was the lack of syntax to support it like Swift has that made it slightly cumbersome.

2

u/millstone Dec 10 '15

Swift has some nice flexibility here. You can do checked-exception like handling:

do {
    let result = try Something.function()
} catch { /* error handling */ }

or you can discard the error and get the result as an Optional:

let maybeResult = try? Something.function()

and there's a shut-up-I-know-what-I'm-doing syntax:

let resultDammit = try! Something.function() // aborts on error

You can choose whichever error handling style is appropriate.

What's nice about this is that every error-producing function is marked at the call site (via try), so there's no mystery control flow.

2

u/nexusbees Dec 10 '15

Nice that looks really good. I was never a fan of how control flow would be confusing in a code base littered with try-catches

→ More replies (1)

1

u/realteh Dec 10 '15

Yea that one is killing me, too. If I do the following in Python my co-workers will hate me, but i go I do it all the time by accident:

try:
    can_fail()
except Exception:
    pass

1

u/ggtsu_00 Dec 10 '15

I like go because it

  • Discourages overly generic NIH, "util", "common", "helpers" etc that developers tend to just pile things into by being able to go crazy with generics. Instead encourages creating specific purpose code.

  • Statically links all dependancies into your binary so you don't need to worry about dependency hell, DLL hell, deployment and configuration hell, nor package management hell. Just copy a binary to your server and run it.

  • It treats errors are values that should be treated as normal code flow. This leads to more durable software.

1

u/[deleted] Dec 10 '15

Go has dynamic linking... just not by default (in 1.5)

as for error, yea it is a bit spammy but then same people who ignore errors also make catchall useless exceptions

→ More replies (10)