News for hackers. Stuff that matters.
Re:Go 1.11 Beta 1 is released by stcredzero on Tuesday June 26, 2018 @07:58PM (#17404860)
I think I just realized something about golang as I was walking down the hall. This is something which confuses many people in a way which many respond to with unwarranted emotion, since they are unable to make this connection.

Since Go uses Duck Typing, having certain methods is effectively a Type Annotation for a given type implementing an Interface. Many programmers, failing to realize this, become outraged at "boilerplate." It is a design trade-off forcing the programmer to specify all cases when implementing an interface, in much the same way that golang's exception handling eschews tools that let you create clever catch-alls and requires you to specify everything. It's just the "no magic" tradeoff.

Re:Go 1.11 Beta 1 is released by jfoutz on Tuesday June 26, 2018 @08:10PM (#17404909)
> requires you to specify everything

That's not my experience. checking for errors is completely optional. I'm probably just a bad lazy programmer that leans to hard on the compiler. I like the catch everything and then refine error handling as i get more understanding of failure modes (i'm from java, i avoid runtimeexception).

Go makes me feel like i need to understand all possible error cases for every line as i type it. I feel like i have to handle it right now, forever, or i'll forget that line can have an error. I really miss being able to gradually refine error handling, because i can rely on the compiler to remind me about all the stuff that can go wrong.

Re:Go 1.11 Beta 1 is released by sagichmal on Tuesday June 26, 2018 @08:52PM (#17405065)
Indeed, Go's position is that your style of programming produces fragile programs, and that it is better to consider the failure modes of each expression as you type it, rather than afterwards.
Re:Go 1.11 Beta 1 is released by erik_seaberg on Wednesday June 27, 2018 @01:59AM (#17406065)

  if err != nil {
          return nil, err
  }
is ceremony that requires no thought and adds no value. There should be sugar for this so it doesn't completely swamp the tiny minority of cases that actually do something.
Re:Go 1.11 Beta 1 is released by sacado2 on Wednesday June 27, 2018 @05:12AM (#17406771)
I actually rarely type that very snippet. In like 3/4 of the cases, I make the error more specific, like

    if err != nil {
        return nil, fmt.Errorf("could not parse file %s: %v", path, err)
    }
the remainder being either the snippet you put above or more complex error management.
Re:Go 1.11 Beta 1 is released by erik_seaberg on Friday June 29, 2018 @04:53AM (#17423282)
You can enrich err.Error(), but when you do you lose the type of the original error and any other fields it may have had. This is something the language can and should do more reliably (i.e., sugar could mutate err.Error() and prepend "MyStruct.myMethod failed: ", or better yet add a damn stack trace).
Re:Go 1.11 Beta 1 is released by cube2222 on Wednesday June 27, 2018 @09:48AM (#17408057)
I'm always using errors.Wrap or Wrapf otherwise it's hard to find the error cause.

I've just created a live template for that in Go, which just puts my cursor in the wrap message to pass along.

Re:Go 1.11 Beta 1 is released by stcredzero on Wednesday June 27, 2018 @12:08PM (#17409317)
is ceremony that requires no thought and adds no value.

Aw, hell no! That's a very easily detectable (anti)pattern! You can scan even a very large code base for that! As noted in other comments, you should be adding some contextual information at this point.

Contrast this with having too-much magic, where you might have absolutely no trace in code left. You can't usefully search a large code base for all such relevant blanks.

Re:Go 1.11 Beta 1 is released by sfifs on Wednesday June 27, 2018 @10:41AM (#17408486)
This is generally a bad idea in any production code. You normally want to add context to the error so that your logging captures the issue at the very minimum. You may want to do cleanup in the function etc. I almost never have written this style of code in many years now.
Re:Go 1.11 Beta 1 is released by jfoutz on Tuesday June 26, 2018 @11:13PM (#17405505)
Seems like breadth first vs depth first search. They will converge at the same answer.

Depth first, you make the happy path work, then refine error handling. Breadth first, you ensure everything is handled at every moment.

I’ll have to think about that a bit. Trivial cases don’t matter. Complex cases are more nuanced.

This is really insightful, thank you.

Re:Go 1.11 Beta 1 is released by sagichmal on Wednesday June 27, 2018 @11:56PM (#17413957)
> Depth first, you make the happy path work, then refine error handling. Breadth first, you ensure everything is handled at every moment.

That's correct. I've heard people describe Go as wanting you to code the sad path first, and then backfill the happy stuff (the business logic) once the error handling is complete. This tracks to my experience. That is, when I program that way, I feel like I'm in sync with the language.

Re:Go 1.11 Beta 1 is released by twiceaday on Tuesday June 26, 2018 @10:31PM (#17405384)
This is the position of the Google c++ style guide, from which I think Go borrowed heavily.
Re:Go 1.11 Beta 1 is released by dilap on Tuesday June 26, 2018 @09:04PM (#17405098)
it's a real trade-off. i think for more interactive software, a "fuck it and throw an exception" approach can be reasonable. for server software it's death. (you need to service the request to the best of your ability, and make sure that you are handling any failures in the best possible way.)
Re:Go 1.11 Beta 1 is released by bigato on Wednesday June 27, 2018 @07:41AM (#17407369)
> i think for more interactive software, a "fuck it and throw an exception" approach can be reasonable.

Your users probably don't think so

Re:Go 1.11 Beta 1 is released by dilap on Wednesday June 27, 2018 @12:13PM (#17409355)
it can be not-so-bad, because it's something like, "oh, i passed in the wrong file" or "oh, that menu item is broken" or something like that. you as a user are entirely in control of the inputs & the state is usually just a file or two, so it's easy to just try again until things work.

but for something running on a server somewhere, many inputs are beyond the user's control & the state is long-lived and fragile (if my HN or FB account gets somehow b0rked, that's a serious problem for me), so it's important to handle all possible error conditions in the best possible way. in my experience this is something exceptions make very hard to do.

(& i would still prefer explicit error handling even for front-end software, but i think the argument for exceptions is at least stronger there.)

Re:Go 1.11 Beta 1 is released by mdanger007 on Tuesday June 26, 2018 @08:16PM (#17404929)
You can throw away errors, by assigning errors or panic with Must. But a goru said, Why not log everything?

Must(func()(val, err)) -> LogIf(func()(val, err))

Re:Go 1.11 Beta 1 is released by dagss on Wednesday June 27, 2018 @02:32AM (#17406168)
...that would be nice except you need to reimplement LogIf or Must again for every single function signature due to lack of metaprogramming..
Re:Go 1.11 Beta 1 is released by sacado2 on Wednesday June 27, 2018 @05:06AM (#17406752)
Then just `panic` first on all errors, and correct it as you go on. This is not really the advised practice, but if it works for you...
Re:Go 1.11 Beta 1 is released by deckarep on Tuesday June 26, 2018 @08:22PM (#17404956)
Go uses structured typing as described by Rob Pike.

Duck typing can still lead to runtime errors which is practically all accounted for thanks to the compile-time safety of structured typing.

Re:Go 1.11 Beta 1 is released by dilap on Tuesday June 26, 2018 @09:05PM (#17405100)
duck typing is an informal term, no? i think it's fair to think of structural typing as "compile time duck typing".
Re:Go 1.11 Beta 1 is released by msiggy on Tuesday June 26, 2018 @09:49PM (#17405242)
don't think so
Re:Go 1.11 Beta 1 is released by stcredzero on Tuesday June 26, 2018 @11:39PM (#17405604)
It's what I meant.
Re:Go 1.11 Beta 1 is released by fbonetti on Tuesday June 26, 2018 @09:41PM (#17405209)
The term you’re looking for is “type inference”, where the compiler determines the type of a variable automatically at compile time.
Re:Go 1.11 Beta 1 is released by ychen306 on Tuesday June 26, 2018 @10:31PM (#17405385)
I think he's referring to not having to explicitly declare if some type implements any interfaces.