Live data from Hacker News

Twelve Go Best Practices

talks.golang.org

151–153 of 153 posts

Re: Twelve Go Best Practices

#151
post #141

Earlier quoted context omitted.

Because the alternative is worse. If you test-and-return after every call, your function has multiple exit points and is far less maintainable. If you nest like this, you at least have a chance of maintaining a single exit point in your function (even though this example fails to do so). This is why the Lord invented exceptions, which it seems that Go does not use. This one example is enough to convince me to never u…

> If you test-and-return after every call, your function has multiple exit points and is far less maintainable. There is nothing wrong with multiple exit points as long as: a. the language has a mechanism for scoped resource allocation (ie. defer, finally, with, unwind-protect or "RAII") b. the early exit doesn't happen in the middle of a long and complex function

> a. the language has a mechanism for scoped resource allocation (ie. defer, finally, with, unwind-protect or "RAII")

Just being pedantic here. RAII is more specific than the defer statement that Go offers. Strictly speaking, defer is not RAII.

Re: Twelve Go Best Practices

#152
post #126

Earlier quoted context omitted.

Where did you get that idea of consensus? A lot of languages do not even have a return statement, neither does lambda calculus. Furthermore, CS community has long abandoned statement based languages in favor of expressions and relations which do not feature "return" for onvious reasons in forms other than equalent to jump.

I'm talking about programmers, not computer scientists. That is, people who actually accomplish things in the real world by hacking on software, rather than pontificating about it from their monadic ivory towers :) The latter have "abandoned statement-based languages," but the former absolutely have not .

I don't see that distinction. The are languages designed to map directly to register machines internal operation, like C or C++ or fortran and those are statement based because that's how the machine works. There are also high level languages, designed to express computation, and those are expression based, again, beacause computation is inherently based on expessions. There are other kinds of languages as well, for other purposes (relations, queries, etc).

Do you really belive that only people working on low level register transfer level things "accomplish things"? That's souds like a very old assembler argument.:-)

Re: Twelve Go Best Practices

#153
post #143
post #23

Interesting that this snippet: func (g *Gopher) DumpBinary(w io.Writer) error { err := binary.Write(w, binary.LittleEndian, int32(len(g.Name))) if err != nil { return err } _, err = w.Write([]byte(g.Name)) if err != nil { return err } err = binary.Write(w, binary.LittleEndian, g.Age) if err != nil { return err } return binary.Write(w, binary.LittleEndian, g.FurColor) } could be written like this: func (g *Gopher) Dum…

func must(err error) { if err != nil { panic(err) } } func (g *Gopher) DumpBinary(w io.Writer) (err error) { defer func() { err, _ = recover().(error) } must(binary.Write(w, binary.LittleEndian, int32(len(g.Name)))) must(w.Write([]byte(g.Name))) must(binary.Write(w, binary.LittleEndian, g.Age)) must(binary.Write(w, binary.LittleEndian, g.FurColor)) return }

That won't work as it is with `w.Write`, as it returns two values, but yes, the idea is perfectly valid.
Post reply on HN