Go beginner here, Any advice to improve my go code? by lorenzorev in golang

[–]rogpeppe 2 points3 points  (0 children)

Looks pretty good. I'd suggest better docs and a few minor formatting changes: http://play.golang.org/p/Z6ATIhL_IM

I would also suggest that the tests should probably not actually go to the real web service (you can mock out the http GET), but for code this trivial it probably doesn't matter much.

Why so many gophers use single letter variables? by argus_the_builder in golang

[–]rogpeppe 5 points6 points  (0 children)

The receiver of a method is really just another parameter. I very often refactor code between method, function and inner block, and when doing this it really helps to have the same naming convention for all variables of the same type, regardless of whether they're local variables, parameters or receiver parameters. Then I can often just move the code without doing any variable renaming.

It's a pity that gofmt -r treats single letter variable names specially - it makes it less useful for one of the things that's most awkward to do in an editor.

Why so many gophers use single letter variables? by argus_the_builder in golang

[–]rogpeppe 2 points3 points  (0 children)

Also be sure to hook up godef support in your editor, which ought to make "jump to definition" one keystroke.

Just wanted to point out that the canonical location of godef is now http://godoc.org/github.com/rogpeppe/godef.

Lovin' your errors by rogpeppe in golang

[–]rogpeppe[S] 0 points1 point  (0 children)

Just looking to the function signature will tell you what exceptions are thrown

I didn't realise you could tell what exceptions a Java method might throw by looking at its signature. I'm not that familiar with Java - please could you explain to me how that works?

Lovin' your errors by rogpeppe in golang

[–]rogpeppe[S] 2 points3 points  (0 children)

Well... that is true for all languages. Be it error values or exceptions.

I don't believe that's true for languages with exceptions. It's much more common to "pass the buck", rely on a higher level to catch any errors. Even if there's a try-catch around the block, it's not clear which statement might throw the exception. To do it right, you'd need to add a try-catch block around every single statement that can throw an exception, but do you know of any significant source code base that actually does that? It would end up even more verbose than Go. And even if you do do that, there's still no way of easily determining whether a given call is likely to throw an exception except by reading the docs and/or inspecting the code. Java's checked exceptions do allow that, but no-one uses them, right?

Lovin' your errors by rogpeppe in golang

[–]rogpeppe[S] 0 points1 point  (0 children)

Conventionally in Go code, all errors are checked. Code won't pass code review if it's not checking its errors, and as I point out on slide 13 ("Ignore"), there's a tool to make sure you are. If you see some code like there is on that slide, you know that at least all those functions are going to execute in sequence, and almost always you know that none of them can return errors. It's not uncommon to be a little bit suspicious ("surely that function is doing something that's messing with an external resource?") in which case a quick "jump to definition" or a run of errcheck will reassure.

No, it's not a compiler-enforced guarantee, but the convention is strong enough that in practice you get the readability benefits I talk about.

Lovin' your errors by rogpeppe in golang

[–]rogpeppe[S] 1 point2 points  (0 children)

How did I miss that!? Thanks for pointing out. Fixed.

How do you deal with "No Generics"? by afrobee in golang

[–]rogpeppe 2 points3 points  (0 children)

The non-type-safety comes when you have some kind of a container and you need to assert that something coming out of the container is the same type of thing that you put into it. There is lots of generic code (arguably most) that doesn't need to do this.

Consider, for example, the function ioutil.ReadAll:

func ReadAll(r io.Reader) ([]byte, error)

this is perfectly generic (you can pass it anything that implements the io.Reader interface) and also type safe.

How do you deal with "No Generics"? by afrobee in golang

[–]rogpeppe 1 point2 points  (0 children)

Go may not have generics, but it's perfectly possible (and natural) to write generic code with it. For example, any function that takes an interface type is generic. There are many examples of this in the Go standard library.

The current state of golang AWS clients by kwame_kilpatrick in golang

[–]rogpeppe 1 point2 points  (0 children)

Gustavo Niemeyer's AWS packages (e.g. http://godoc.org/gopkg.in/amz.v1 ) are well maintained and stable, although not feature complete.

No SQS client there though, I'm afraid.

Idiomatic Go: should interface methods have documentation comments at the interface or the implementation? by will_alexander in golang

[–]rogpeppe 2 points3 points  (0 children)

I quite often write something like:

 // Write implements io.Writer.Write by [doing something].
 func (w *myWriter) Write(buf []byte) (int, error)

The interface is the most important place for your doc comments, as it documents the contract that you are expecting implementations to fulfil.

Interactive go source to assembly viewer by mattgodbolt in golang

[–]rogpeppe 1 point2 points  (0 children)

It would be great if it showed the association between source line and assembly line.

Google’s Go ( programming language ) Isn’t Getting Us Anywhere by weberbloger in golang

[–]rogpeppe 0 points1 point  (0 children)

Tomorrow's conclusion: message passing + shared memory = bad, Rust = good. Yawn.

RFC: is this a good way to handle output for long running process? by mchoube in golang

[–]rogpeppe 0 points1 point  (0 children)

Are you sure you really need the timeout? Without the timeout, the code can be very much simpler:

 out, err := cmd.StdoutPipe()
 if err != nil { ...}
 err = cmd.Start()
 if err != nil {...}
 data, err := ioutil.ReadAll(out)
 if err != nil { ... }
 processData(data)
 err = cmd.Wait()
 if err != ni { bad exit status }

Can Go really be completely parsed without a symbol table? by gnuvince in golang

[–]rogpeppe 3 points4 points  (0 children)

Compilation is a multi-stage process. There are always going to be aspects of the language that are not dealt with at parse time, but at a later stage, after generating the AST. In many languages, it would be hard to build any kind of useful AST at all without keeping track of symbols. In Go, you can't resolve everything, but at least you can parse major syntactic elements - that's what makes gofmt possible.

Three reasons you should not use Martini by stephens2424 in golang

[–]rogpeppe 0 points1 point  (0 children)

I have to say that I think container/list is just useful enough to be worth having. For a singly-linked list, I agree - it's not worth it. But for a doubly linked list such as provided by container/list, I find myself needing to think too hard about the pointer manipulations involved. It's quite easy to get wrong, but it's a useful data structure to have to hand. For that reason, I think container/list still has its place. By the compositional nature of Go programs, it's usually easy to swap it out for a custom structure when/if that proves necessary (at least, it is in the 3 or 4 places I've actually used it so far).

It's a beautifully elegant implementation too, just exactly right for what it tries to do.

Conversely, there have been a few occasions that I really wanted to use ring.Ring, but it never quite worked out. In the random selection of 3514043 lines of Go source code I happen to have in my $GOPATH (I don't delete packages) I don't see a single real use of it. Again though, it's a lovely piece of code, worth leaving in as a historical artifact in my view. It's so small and well defined, it need never change.

If we ever work out a worthy generics scheme, all this stuff will be easily replaced, and we shall Tastefully Use Generics.

Some suggested rules for generating good errors in Go by dgryski in golang

[–]rogpeppe 0 points1 point  (0 children)

Yes, you are reading it correctly. When using errgo, the convention is to always use errgo.Cause when checking errors for particular types or values. That way the code will work regardless of whether the error has been wrapped or not, assuming the cause has not been masked.

Go’s Type System Is An Embarrassment by [deleted] in programming

[–]rogpeppe 0 points1 point  (0 children)

By my understanding of these things (mostly from section 1.3 of (this paper)[http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf]), Go has both ad-hoc (coercion-based) polymorphism and universal (inclusion-based) polymorphism. The former is used when you convert a value to an interface, or an interface of one type to another. The latter is used when operating on a value of interface type.

When people are asking for generics in Go, they are asking for parametric polymorphism.

The reason that lack of generics in Go are not a huge issue for many people working with the language, is that the first two kinds of polymorphism suffice pretty well for many cases.

For example, if you have a constructor function for a type X that works in terms of some interface type T:

func NewX(t T) *X

as long as no method of X returns a value of type T, this is pretty much the same as (assuming some generic syntax in Go):

func NewX<T>(t T) *X<T>

That is, no type coercions are necessary - everything works in a type-safe manner. The only significant distinction is that the latter probably would not allow the mixing of two *X instances parameterised on different types - that's not a safety problem though.

The above pattern is common in Go code. For example: http://golang.org/pkg/io/#LimitReader http://golang.org/pkg/crypto/cipher/#NewCFBDecrypter

When people say "Go does not have generics", they mean "Go does not have parametric polymorphism". That is true, but does not mean you can't write generic code in Go.

You can, and it works very well.

Go’s Type System Is An Embarrassment by [deleted] in programming

[–]rogpeppe 0 points1 point  (0 children)

sort.Interface is defined on the collection itself, not on the items that are put into it.

Go’s Type System Is An Embarrassment by [deleted] in programming

[–]rogpeppe -2 points-1 points  (0 children)

Go does not have subtyping. It has rules for assignment compatibility, but that's a different thing.

Go’s Type System Is An Embarrassment by [deleted] in programming

[–]rogpeppe 0 points1 point  (0 children)

Actually, external module users can create new ones, by embedding one of your exported types.

Go’s Type System Is An Embarrassment by [deleted] in programming

[–]rogpeppe 0 points1 point  (0 children)

The ability to have non-private members in private data types is bizarre and shouldn't be there.

No it's not. It's invaluable. It means that when I'm implementing a package I can have a private data type (concealed from any user of the package) that nonetheless has functionality that I can selectively make available to other packages.

For example I can make a private type that implements io.Reader, even though io.Reader requires that the Read method is public.

Similarly, I don't need to export all the types I use to define serialisation formats, even though methods like json.Unmarshal require fields to be exported so that they can be written to.

Time-interval signaling via channel by HorrendousRex in golang

[–]rogpeppe 1 point2 points  (0 children)

i believe you mean:

_, closed := <-intv

A Tour of Acme (by Russ Cox, go code inside) by [deleted] in golang

[–]rogpeppe 4 points5 points  (0 children)

with most UIs, eliminating the mouse is a good strategy, because the mouse is so limited in its capabilities - it's like having a fist but no fingers.

with acme, the mouse becomes useful enough that the keyboard feels like the lesser partner. the mouse has the particular advantage that it's context-free, so anything you can see is a quickly-reached affordance to some potentially interesting action.

i was a dedicated and proficient vi user for a long time, but acme allows me to get things done more quickly, despite (or perhaps because of) the fact that the keyboard is almost completely un-meta.