10 ms·
Understanding Go panic output
- VeejayRampay 9y agoAs a not-yet-experienced Go programmer, I have to say it'd make it way more palatable if the reference to the error in my code could be somehow extracted and displayed in a more obvious fashion. I've had panic happen in Go code before (most of the time when trying to read the body of a http response that happened to be nil for whatever reason) and I was kind of lost in a trace of errors happening deeper in the standard library that I mostly didn't care about at first sight (until a few lines down, I could find a reference to my own program). That being said, Go is still a pretty good language when it comes to tooling and error handling / reporting.
- mellett68 9y agoReally this is my main complaint with most languages/tools when it comes to runtime errors. This is especially painful when you introduce frameworks and things that create so much misdirection that the call stack can be enormous.
- laumars 9y agoTo offer a counterargument, I've always considered Go's panics to be pretty readable because you can read the top line to get the error and then one of the next lines down (I forget which but after a while it tends to stand out) to see the line where the error was raised. The rest of the stack is if you need to trace the error back through the calling routines (I rarely need to do this but it's useful if the panic isn't being raised by your own code). Granted things get a little more tricky when you start multiplying the number of concurrent goroutines. I've sometimes ended up with several terminal screens full text. But so long as you can find the start of the panic output then you can generally find what you want without too much difficulty.
- tomsmeding 9y agoHave you ever looked at typical template compiler errors in C++? The same problem occurs, I suppose, but then much worse. I regularly have multiple screens of text where your own single line of code is hidden like a needle in a haystack. Clang seems to do this _slightly_ better than gcc, but it's both bad.
- amelius 9y agoNot a gopher, and wondering why there are no proper exceptions. "Panic" sounds like there's no way out other than aborting the process.
- laumars 9y agoPanics can be caught and handled by parent callers and thus behave a little bit like exceptions (not as pretty I'll grant you, but still better than the "no way out other than aborting the process" approach it seems at first glance). However you generally you don't want to catch panics except under rare conditions because panics are intended to be used in "the world is ending" kind of scenarios (it doesn't always work out this way but that is the theory). For general error handling there is an error object that gets returned by functions. It leads to the often moaned about syntax below: if err != nil { return err } Yeah it's ugly. Yeah other languages have try / etc. But for all of it's sins I've found it still gets the job done.
- amelius 9y agoThanks for the explanation. Writing that if-construct after every function call doesn't sound like a lot of fun. In fact it sounds like work! Honestly, I think I'll wait until Go has matured a bit, before I try it for a side-project.
- konart 9y ago>matured a bit If it is not mature enough for you now - it will never be. Right now it is already used in many huge IT companies and extremly popular in China. Writing that if-construct after every function is the design, not the lack of it.
- laumars 9y agoIt's easy to criticise this from a visual glance but you have to remember that proper error handling in any language is hard work and often produces ugly code where you need to account for multiple edge cases - many of which aren't serious enough to cause an exception but would still break the program's logic. All Go does is group these conditions into a standard interface. Furthermore, because Go does unify these conditions, I've found it effective for handling a whole array of differing edge cases that I might have otherwise overlooked. In that regard I've anecdotally found it more effective than most of the habits you see in other languages (I can't say all languages because I haven't used every language out there, but I have developed in excess of a dozen different programming languages over the last 30 years). In short, in Go error handling becomes a first class citizen rather than a byproduct you begrudgingly need to check for. Also while it might not be to everyone's tastes, adding "if err != nil {" really does add practically nothing to your overall dev time in real terms. But I'm not here to advocate one method of error handling over another, nor to try and convince anyone to use Go who might not have otherwise. Obviously you can see I get a little frustrated when people judge it purely on face value but that's because I have spent some time with the language and found it to be surprisingly effective at delivering a stable product.
- 0xFFC 9y agoSorry for this unrelated question. I am not very familiar with Go ecosystems. I have one question why there is not official debugger for Go? P.S. I am familiar with delve. I am looking for official debugger support.
- cdoxsey 9y agoPart of the reason has to do with Go's original primary use case: a server handling thousands of requests. In that environment a traditional debugger, at least in my experience, isn't super useful, because it models a single-stepped workflow but the program is massively concurrent. (also pausing a production app is probably a bad idea) The kinds of bugs you run into are different and harder to track down, so Go has invested resources into some of those problems (for example the race detector, and automatic detection of concurrent map misuse) and you tend to rely on logging, profiling, tracing and stats to solve bugs. I think there are plans to make an official debugger it just hasn't been prioritized.
- konart 9y ago>In that environment a traditional debugger, at least in my experience, isn't super useful, because it models a single-stepped I'm pretty sure the question was about debugger to be used during development.
- joeshaw 9y ago> I think there are plans to make an official debugger it just hasn't been prioritized. There was a nascent project inside the Go team a couple of years ago (called ergo I think?) but I believe it was shelved in favor of Delve, which was much further along. It's probably safe at this point to consider Delve the de facto standard Go debugger.
- deleted 9y ago[deleted]
- Nvorzula 9y agoOn the whole I am very happy with Go, but this did bite me recently. We're using the X509 package which is returning an error on a handful of CA certificates due to characters that are not technically allowed in an ASN.1 PrintableString field (E.G `&`). Sure, fine, we've already forked the package so it's easy to make the change to be more lenient. Except errors don't contain any information about the origin of the error in source code, so it's been down to painful debugging and source code digging to find where this check is being made. If anyone has a less painful way to do this, please halp.
- mememaestro 9y agoTo y'all gophers out there https://www.youtube.com/watch?v=kaWa4rMYNCg https://www.youtube.com/watch?v=kaWa4rMYNCg
- Aissen 9y agoOr, you could use panicparse: https://github.com/maruel/panicparse https://github.com/maruel/panicparse
- obeattie 9y agoNice article, thanks for publishing this. However: >This panic is caused by dereferencing a nil pointer, as indicated by the first line of the output. These types of errors are much less common in Go than in other languages like C or Java thanks to Go’s idioms around error handling. I often see assertions made like this about Go. My experience at companies which use Go as their primary development language with reasonably-sized teams (30-50 people working on hundreds/thousands of Go programs) does not agree with this. I would say nil pointer dereference errors are just as common as if they were writing code in C or Java. I don't have hard data to back this up though; perhaps you do.
- icholy 9y agoIt happens way more often in java because all references are nullable. In Go, unless you're dealing with a pointer, it's not going to be nil.
- bsaul 9y agoNot a lot of experience in go, but I’d say deadlocks ( yes, even with channels), and nil pointers are indeed the two most common errors i encountered. Go do makes things easier, but not fail proof.
- notheguyouthink 9y agoI think it depends on what you're doing. In my old org, we often avoided pointers. Or rather, we used them when we needed them. Because of this, I'd wager easily 50% of our codebase was not using pointers at all and was completely type safe. Though regardless, after using Rust for a while, i hope we can find a way to bring that safety to Go. I use a compiler and not JS/Python/etc for a reason, after all. I want compile time safety, all the time.
- LaFolle 9y agoOne of the most common errors that i used to encounter when i first started with golang was- accessing a map without initializing it first. For example: var m map[string]int m["name"] = 23 which resulted in "panic: assignment to entry in nil map". Nil pointer access errors are relatively easy to debug when in Go world, as compared to in CGO. Like, we are using zmq via CGO in one of our project and when these errors occured, we had to use gdb to debug (apart from logging). At one time we even used rr. rr is really cool, everyone should check this out. https://github.com/mozilla/rr https://github.com/mozilla/rr
- Walkman 9y agoMaybe the go panic output could be a little bit more helpful.