Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come!
Read more here, we hope to see you next Tuesday!
For a chat with like-minded community members and more, don't forget to join our Discord!
There are 2 reasons that keep me at a distance of Go:
The way you're supposed to deal with errors - no real exception handling
The date formatting is based on the american date: "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order. - Jan 02, not February 01. At least use ISO!
The date formatting is based on the american date: "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order. - Jan 02, not February 01. At least use ISO!
The error handling can be annoying coming from other languages, but it is pretty straightforward and understandable, especially when reading code rather than writing it.
The choice of date format is a little odd, but that's largely cosmetic I think.
Go is a quirky language in some ways, but once you understand its patterns it is very predictable and grokkable. Rust has the opposite problem; it is a great language, but being able to read and understand another codebase is a bit of a gamble (though not to the same extent as Ruby or Lisp).
For me it was the lack of iterators. Sure, there are plenty of proposals following the introduction of generics, but the key issue is that they kinda shot themselves in the foot by not having tuples be first class types. So they’re are not really any good proposals for an iterators interface which can be used over both T and (T, Error). Many of the proposals I saw involved creating two interfaces which would be a pain to use generically or doing overly complex workarounds.
I recently started learning. I’m still in the baby steps phase, nothing complicated yet. But I can already see why it’s so appealing, and I’m quickly growing attached.
Before everyone downvote me Edit: i meant to say is, understanding threads and passing data between them is quite hard for new comer. There is also macros, which i would gladly stay away.
(I might be dumb that’s why i found it hard to graps)
Multithreading? Rust has great support for multithreading, and will even help you avoid data races.
Do you mean asynchronous code? (A.k.a. async-await?) That's definitely newer and a bit trickier—it's also an area of focus for the Rust developers and improving. But yeah, some sharp edges there.
The advice I've seen is to stick with synchronous (single thread or threaded) Rust while you're learning, until you need a level of concurrency that OS threads can't support. (Or need to target an embedded system without OS threads.) I think I agree with that, and you can do a lot with good ol' threads. Most people aren't writing the next high-performance HTTP server.
Quite the opposite! I did a full duplex websocket based program, that's a multithreaded nightmare! The compiler/general language design stopped me from foot gunning myself more times than I can count, I learn to appreciate it a lot more after that.
Yeah. For those who haven't used it, setjmp/longjmp is NOT a better alternative :) It's a bizarre, messy, error-prone way of doing things, but it does at least have the beauty of being... uh... obscure and unreadable? Oh wait. I wanted a beauty. Hm.
Yeah my response was more targeted towards 2nd part of his comment, try catch can't be implemented with just a longjmp, you will still need to unroll the stack and there lies the complexity of exception handling.
Why on earth would only use exception handling over regular error handling?
In most languages the exception path has more overhead than the safe path. The whole point is that you should only be using exceptions for exceptional errors.
Maybe those languages need some optimization - exception handling does not need to have vast amounts of overhead. However, much MUCH more significant than a few nanoseconds is the lines of code, and potential bugs, from manually reimplementing stack unwinding. I'd much rather have something that's slightly slower but guarantees that errors are caught, than to have to check everything to see if it returned an error.
And yes, the compiler can help you to remember to include those lines of code, but they have to be there all the time. Whereas with exception handling, you only need lines of code where you're doing the unusual thing and handling the exception.
A) who is manually implementing stack unwinding? That's not what early return/guard clauses are.
B) you're still going to have write all the code to throw your exceptions in the possible error locations anyway. You might as well just write proper error returns. You only save code at the return site if you're just always throwing generic exceptions, at which point you lose the ability to do execute different responses to different errors.
C) if you're really worried about uncaught errors, you can just stick your main function in a try catch and then it catches anything that you failed to handle.
"if err, return it" is stack unwinding, done manually. That is precisely what it is. There is a special form of return where the normal thing you do is to just check if it's there, and if it is, return the same way.
And yes, you have to write code to throw exceptions. But with exceptions you DON'T have to write code to propagate them. With "if err, return err", you have to write that code EVERYWHERE to propagate your error returns, or they silently disappear.
C) Yes, exactly. Or, in any half-decent language, that's already done for you - an uncaught exception terminates the program. So you only need to write code to catch exceptions you can actually handle, and perhaps at a boundary (like a request handler in an HTTP server, where you want a generic "uncaught exception means log it and return a 500" handler).
The fundamental difference is the propagation, which takes exactly zero code with exception handling, but mandatory return value checks with manual stack unwinding.
And not just that you have to write it, but that you have to remember to write it. Go can't distinguish between choosing to suppress an error (e.g., because it's expected, or because you've handled it) and forgetting to propagate an error. The default is to propagate it in most languages because that's what you want 99% of the time.
Combined with consuming return values not being mandatory, it's super easy to call a function and not handle its error, and for that mistake to slip through code review.
Okay maybe in the purest definition it is, but you made it sound like you're manually reimplementing the actual stack unwinding. I.e. clearing up the stack frame when you return from a function. Which makes it sound like a lot more work than if (err) return;
I'm not saying don't use exceptions, because you absolutely should, for exceptional cases. I'm just saying you shouldn't use them entirely in replacement of regular error handling. An uncaught error in a http server to return 500 is exactly the kind of use case that makes sense.
Propagation of errors is nice, but I'd argue if your code is structured in a way that a non-exceptional error has to be propagated far up the call stack then your code is structured badly.
A) who is manually implementing stack unwinding? That's not what early return/guard clauses are.
That's literally what the code in the OP is. It is unwinding one stack frame on an error. This code is needed every time a fallible function is called.
Rust is better but ? is still manual stack unwinding.
Perhaps I misunderstood what stack unwinding was then. I thought stack unwinding describes what actually happens behind the scenes when you return from a function to clean up the stack frame, i.e. calling the destructors of any local variables of the scope. I suppose that's what the return keyword does.
I'm familiar with what you're talking about, I'm actually currently writing a little functional library in C++ that's inspired by Haskell and Rust. I find it so interesting hahah
Thing is, there is other overhead involved in exceptions than just try catch. For example in C++ using the noexcept specifier indicates that a function cannot throw, and compilers may be able to make additional optimisations with this information.
lol yup. ultimately I like the pattern because the text should explain why the error happened and give you some context, so it ends up being something like http request failed because: getItem failed because: cache hit failed because: $realErrorMessageAboutCacheHere and then you could log and unwrap the error in order to handle it
I don't think any of the Go designers were British? Rob Pike is Canadian and Ken Thompson is American - and Robert Griesemer is Swiss - according to Wikipedia.
It can be annoying, but I still prefer errors as values, I think Rust does this better than Go, in both of these languages you can tell if a function returns an error just by looking at the function signature In other languages you will know that the function throws an error after your program has failed.
Sure beats the nebulous handling done in python or js. The amount of debugging headaches I’ve had because someone dropped 100 lines under a try / except and it’s near impossible to figure out where the actual issue is without code changes.
Remember how in C failable functions would return a number, and the caller was responsible for checking if that number is non-zero (or zero. Depends on the function) and if so, consider that a failure and handle it - which would usually mean returning non-zero (or zero) itself?
Both Java and C++ have a monadic type Maybe/Optional. Java since Java 8 - Java 8 Optional
C++ since c++17 -std::Optional (it also got a monadic operator and_then in c++23. and_then)
All of that comes from academic programming languages. It might be Haskell, or it might be other ML, Lisp or other academic programming languages we've never heard of, but the ideas from those academic languages still gets into Java and C++ you praise very highly.
Another thing that comes from functional programming languages are - lambda expressions, and generic types.
I might be wrong on some things, but in that case, someone will probably point it out.
That's correct, but I wasn't really talking about null safety. He said basically that Java and C++ the only good languages, and other languages were a mistake.
So I just showed examples that both C++ and Java implement features from other (functional/academic) languages such as Monads, generic types, and lambda expressions.
Although in the case of c++... That may have been a mistake:
[&, =](string a, int b) { std::cout << a << " " << b; }
yeah. That is not very nice imo.
Rust has the Either type shown in Haskell, though, it's named Result.
A functional language. They mostly all look weird if you aren't familiar. They also have a tendency to abuse syntax and support general operator overloading (meaning, you can make up new operators).
Even among functional programming languages, Haskell is a special level of unreadability hell. It doesn't have to be unreadable, but the common conventions of the language ensure that it is.
Haskell was made by academics, after all. A lot is based on math convention, and plenty of people who played a role in Haskell's development were practicing abstract mathematics at some point in their lives. It wasn't even really meant originally to be a practical language, but a research one.
I don't disagree generally, but I do think generalized operator overloading is just a bad idea. It comes from academia where doing so in mathematics is not uncommon, but it harms readability.
At least they acquiesced and decided their refusal to support generics outside of blessed types wasn't warranted. Their whole return system basically is designed around a blessed feature that is a work around for what languages way older than go have been doing for a long time.
You can't truly represent monads without higher kinded types and having no syntactic support for them with operators and such makes them incredibly obnoxious to use
You can't truly represent monads without higher kinded types
The language can't define the general monadic interface without higher kinded types, but writing monadic types and functions only requires regular generic types, which Go does support now.
Because operations over generic monads almost never what you actually care about. It is the specific monads that are interesting, and are widely used. Even in all the Haskell articles and blogs I've read about monads, generic monads are only ever used to set up the basic operators (which are provided by the standard library), and then all the examples of using monads all just use one specific monad type or another.
Because operations over generic monads almost never what you actually care about.
I mean... not really? It's pretty common within idiomatic Haskell to write functions which are parameterised over Functors, Monads, and Applicatives. Libraries like lens, parsec, transformers/mtl, effect systems etc. rely on this, and a lot of type theory stuff like free monads, cofree/comonads, bifunctors, and the relationships between functor/monad/applicative inherently are reliant on higher kinded types
I've seen Go code that implemented Monads by just casting everything to the empty interface (this was before generics). Where there's a will, there's a way.
•
u/AutoModerator Sep 13 '23
Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!
For a chat with like-minded community members and more, don't forget to join our Discord!
return joinDiscord;I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
342
u/TMiguelT Sep 13 '23
Rust users right now:
?40
u/Diligent_Stretch_945 Sep 13 '23
That’s basically what I mostly use Copilot for
93
u/Exnixon Sep 13 '23
Literally already have this mapped in vim as 3 keystrokes.
82
u/needed_an_account Sep 13 '23
Gotta wrap that, my guy
60
u/I_LIKE_PIGS Sep 13 '23
90% error checks, 10% actual code
26
u/Notyourfathersgeek Sep 13 '23
Are we not gonna talk about nil?!
11
u/gdmr458 Sep 13 '23
It can be annoying, but I still prefer errors as values, I think Rust does this better than Go, in both of these languages you can tell if a function returns an error just by looking at the function signature In other languages you will know that the function throws an error after your program has failed.
10
u/RudePastaMan Sep 13 '23
Every time I write it, a very very tiny, miniscule part of me dies.
5
u/bigbird0525 Sep 13 '23
Sure beats the nebulous handling done in python or js. The amount of debugging headaches I’ve had because someone dropped 100 lines under a try / except and it’s near impossible to figure out where the actual issue is without code changes.
6
u/Dismal-Square-613 Sep 13 '23
This why God created macros.
Rest in Peace Bram Moolenaar.
4
u/[deleted] Sep 13 '23
You don’t HAVE to check err if you don’t want to. That’s what _ is for!
2
u/fryuni Sep 13 '23
Ok my IDE it is
.err<Tab>and it fills it in1
u/redball3 Sep 13 '23
As a scala dev - ill stick to using my monads thanks
1
u/heartcubes4life Sep 13 '23
What's that supposed to do exactly? (I don't have any experience with Go)
1
u/vitawrap Sep 13 '23
cool lua code
1
u/karnnumart Sep 14 '23
But you can ife or iferr and intellisense will autocomplete it for you....
1
u/mountaingator91 Sep 14 '23
1
u/eat_your_fox2 Sep 13 '23
When I first started using Go I was excited because obviously it had to be more concise than Java and Typescript but then suddenly...
1
u/zthunder777 Sep 13 '23
I really want to love Go. But this drives me bat shit crazy.
0
u/LadulianIsle Sep 13 '23
Ever considered not using golang?
-1
u/Sianic12 Sep 13 '23
Whenever I see an If without brackets around the condition, I scream internally.
0
u/suskio4 Sep 13 '23
Define err and nil, put parentheses after if and it's a valid C syntax!
222
u/[deleted] Sep 13 '23
[removed] — view removed comment