3 ms·
I extracted out all the err-checks up to where they're handled. if err != nil { return nil, err if err := json.NewDecoder(response.Body).Decode(&data
by mrkeen 3y ago
I extracted out all the err-checks up to where they're handled.
if err != nil {
return nil, err
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
return nil, err
if err != nil {
return err
if _, err = reader.Read(); err != nil && err.Error() != "EOF" {
return err
if err != nil {
if err.Error() == io.EOF {
break
}
return err
if err != nil {
fmt.Printf("Failed to fetch data from %s: %v\n", url, err)
continue
if err := processCSV(csvFile); err != nil {
fmt.Printf("Error processing CSV: %v\n", err)
Everything just bubbles-up. You've reinvented Exceptions with more boilerplate. Nothing here is 'handled' beyond that.
Although I don't read Go, it looks as if main() returns success to its caller even in the failure case.
So the Python code "which doesn't handle exceptions" exits in failure properly with a good error message and stack trace. The Go code "which handles all aforementioned errors" returns success for a failed operation, printing a less clear error message than the Python.
- pdimitar 3y agoYou're getting hung up on details of code written by ChatGPT, you do realize that, right? It's a singular line change to return proper OS exit code on error in the main function. And what error message is clearer is not an objective fact but only a matter of preference.
- mrkeen 3y ago> You're getting hung up on details of code written by ChatGPT, you do realize that, right? I'm getting hung up on what the author chose to publish. The author asked, >> do you want to know when something bad can happen in your program, or don’t you? and then published some Python code and some Go code. Of the Go code he wrote: >> Wouldn’t you know it, we have all the aforementioned errors handled!" It's not some subtle gotcha, it's the point of the article and he somehow managed to demonstrate the opposite. It would be like switching from garbage collection to C to make sure you remember to free all your mallocs, then showing demo code that's only a singular line change away from returning all its memory.
- pdimitar 3y agoI still maintain that this code is one line of fix away to satisfy your requirement of a proper OS exit code, which you didn't address in your comment. And readability. It's quite subjective and I can work with both the Python add Golang errors.
- iamcalledrob 3y agoTo be fair, this is horribly written error handling. These errors should be wrapped to add the specific context that the caller might want in order to handle the error.
- mrkeen 3y ago> To be fair, this is horribly written error handling. I'll can only take it as it's presented, in good faith. The author chose what to publish and compare. If I've been trolled, so be it. > These errors should be wrapped to add the specific context that the caller might want in order to handle the error. That would be an Exception and its stack trace.