Live data from Hacker News

Twelve Go Best Practices

talks.golang.org

51–60 of 153 posts

Re: Twelve Go Best Practices

#51
post #38

Earlier quoted context omitted.

Less than 5% of your coworkers understand decorators? I don't want to sound snooty but this is a pretty trivial application of higher order functions.

I have to agree; it's basic stuff even for a Python weenie like me.

It's basic stuff because you're a Python weenie; I don't see much use or discussion of decorators outside the Python world.

Re: Twelve Go Best Practices

#52
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…

They do something interesting in the 'template' lib, they have a func called Must which will eat the error and throw a panic if you don't want to handle each error yourself, I like it because it's much more explicit than the func just throwing the exception without you knowing.

http://golang.org/src/pkg/text/template/helper.go?s=576:619#...

Re: Twelve Go Best Practices

#53
post #29

Earlier quoted context omitted.

> Didn't read, because mouse scroll wheel doesn't work. You don't need the scroll wheel to navigate the presentation. Click on the right side of the slide to go forward, on the left side to go back. > Honestly, who thinks this stuff is a good idea? People who are making presentations to deliver in an in-person setting where putting them on the web for everyone is a secondary use, not the primary use? I mean, looking…

Also, you can use the arrow keys to go back and forth through the slides.

Doesn't work super well on mobile though.

Re: Twelve Go Best Practices

#54

Odd choice of examples... 1. The file I/O makes the case for including exceptions in the language. Specifically, adding one-off types to deal with exceptions is a bug, not a feature. There is a good case against exceptions but that ain't it. 2. On slide 5, it appears to show that you have to use a switch statement on a generic to get polymorphism because the language doesn't support overloading. Again, looks more lik…

> The file I/O makes the case for including exceptions in the language.

Exceptions are included in the language, they are called panics, and the go convention is that libraries don't expose them in the public interface, but can use them internally (and, of course, application code can use them.)

> Specifically, adding one-off types to deal with exceptions is a bug, not a feature.

One-off types aren't used for error handling in the example, they are used for abstracting a writing pattern that works like binary.Write for writing non-string values and like io.Writer#write for string values. Sure, the special type's write method also swallows errors, but, except that the mechanism by which it swallows errors would look different, the use of the one-off type and its write method would be pretty much the same with exceptions/panics as with error returns from the underlying library functions.

Re: Twelve Go Best Practices

#55

Earlier quoted context omitted.

I have to agree; it's basic stuff even for a Python weenie like me.

It's basic stuff because you're a Python weenie; I don't see much use or discussion of decorators outside the Python world.

They might not be called decorators, but they're also used in JavaScript and other languages with higher-order functions.

Re: Twelve Go Best Practices

#56

Earlier quoted context omitted.

I have to agree; it's basic stuff even for a Python weenie like me.

It's basic stuff because you're a Python weenie; I don't see much use or discussion of decorators outside the Python world.

Decorators are a special Python syntax for this use, the more general term for what is going on is "higher-order functions".

Which are pretty important, and not specific to python.

Re: Twelve Go Best Practices

#57

Earlier quoted context omitted.

I have to agree; it's basic stuff even for a Python weenie like me.

It's basic stuff because you're a Python weenie; I don't see much use or discussion of decorators outside the Python world.

For what it's worth, this is also a very common pattern in ruby, using blocks:

  def with_error_handler
    if error = yield
      puts "error: #{error}"
    end
  end

  def do_things
    error = do_this
    return "error doing this: #{error}" if error
    error = do_that
    return "error doing that: #{error}" if error
    nil
  end

  with_error_handler do
    do_things
  end
Having said that, I did need to read the Go version more than once to grok it. I theoretically like Go's syntax for defining functions that take functions, but in practice I find it quite hard to scan, especially if there are more parameters on top of the function, or the passed function has multiple returns, or (god forbid!) it takes a function itself - it can all become quite a lot of bookkeeping.

Re: Twelve Go Best Practices

#58
post #20
post #12

Earlier quoted context omitted.

Even the correct version is not that good. The multiple checks on nil is an obvious pattern and as such should be abstracted away. Haskell does it with Maybe but as long as you have function as first class object, it should be doable. Like in Python, Ruby, etc.

The very next slide cleans it up using a "utility type" which reminds me of the "monadic" Haskell solution.

Absolutely, I'm going through some of my code now where I had thought of the first improvement as obvious but hadn't considered that second, much nicer solution in Go. It really is like making a custom Maybe type. It might be another pattern to be improved on to have tons of these sorts of custom types running around, though...

Re: Twelve Go Best Practices

#59
post #14
post #2

If #6 is among best practices, I'm sad. I could take a dynamically-typed language instead. http://talks.golang.org/2013/bestpractices.slide#6

They allow dynamically typed methods when you need them. Why does that make you sad?

Loss of strong typing and runtime type detection is what makes me sad.

Well, Go 1 has some of the problems of Java 1: no generics, typecasts from interface{} here and there, simplistic GC. Reasons are probably similar: this all is good enough for version 1, and can later be improved upon.

Re: Twelve Go Best Practices

#60

Odd choice of examples... 1. The file I/O makes the case for including exceptions in the language. Specifically, adding one-off types to deal with exceptions is a bug, not a feature. There is a good case against exceptions but that ain't it. 2. On slide 5, it appears to show that you have to use a switch statement on a generic to get polymorphism because the language doesn't support overloading. Again, looks more lik…

> The file I/O makes the case for including exceptions in the language. Exceptions are included in the language, they are called panics, and the go convention is that libraries don't expose them in the public interface, but can use them internally (and, of course, application code can use them.) > Specifically, adding one-off types to deal with exceptions is a bug, not a feature. One-off types aren't used for error h…

My point is that an example of a 'best practice' shouldn't make the reader think: Oh, that's a kludge to get around a design decision in the language.

In his example, he uses a one-off type to isolate the caller from having to explicitly check if each individual write failed. I got no problem with that.

But it seems like it a work-a-round.

It's just an odd choice for an example. The take-a-way seems to be that the basic class libraries need a wrapper that makes them easier to work with and that, you the developer, should build these wrappers so you know exactly what the policy is.

Post reply on HN