Live data from Hacker News

Stages of learning Go, with code examples

sourcegraph.com

71–80 of 118 posts

Re: Stages of learning Go, with code examples

#71
post #51

Where does this idiotic idea cames from that being "idiomatic" is some kind of virtue? Idiomatic Java was the FactoryProxySingleton EE-wank fest from Apache, SUN, Spring and co. It was by changing those idioms around (e.g. how the Play framework wrote Java), that saner alternatives emerged. The de-facto idiom of PHP before 2010 was messy code. It started getting more coherent after that, but the preffered idiom now (…

Where does this idiotic idea cames from that being "idiomatic" is some kind of virtue?

I can't say for sure, but I'm guessing it probably came from years of experience working on the sorts of projects that Go was designed to address. Go is really geared toward large distributed projects with a lot of contributors. In that context, it makes sense to value some sort of conformity. It may not always result in the most efficient code, but it makes the code much more maintainable and accessible to newcomers. Once you're familiar with idioms you don't need to spend so much time figuring out what a new piece of code is trying to accomplish.

You seem to argue that since old PHP and javascript code had terrible coding practices, therefore Go shouldn't try to enforce any particular type of practice. I don't see that your conclusion follows from that premise. Now if you're arguing that good coding practices arise from the community, that's well and good. Idioms can change and I expect that idiomatic Go code in 2020 will be different from idiomatic Go code today. That doesn't mean there isn't value to writing idiomatic Go code today.

I haven't actually seen any cargo cultists explicitly say that no one should be writing in other programming languages for any project. That would be a pretty stupid sentiment, although I shouldn't be too surprised if some people felt that way. Go's strength is really on server side code where storage is cheap and network and I/O latency is a bigger issue than CPU or memory utilization. If you're developing for that kind of environment, there's a lot to be said for using Go. And while you're at it, you'll save yourself and anyone else who's working with your code a lot of hassle if you follow best practices and write idiomatic code most of the time. If you're in a specific situation where you can get better performance or whatever by deviating from the standards, then go for it, but leave some comments explaining your choices to anyone else who has to maintain your code.

If I'm going to write a one-off script to process a million lines of text, I'm probably going to write it in Perl, not Go (and yeah I know it's 2015). My Perl code will follow TMTOWTDI whatever-the-heck-works practices. If I'm already writing Go for something, and others might want to peek at my code, then I will make an effort to make my code idiomatic.

Re: Stages of learning Go, with code examples

#72
post #34

Earlier quoted context omitted.

Generics and composability are orthogonal concepts, to me. I'm not really sure what sort of composability you're talking about. Can you provide an example for me to rip apart?

Anything to do with higher-order functions, least of all compose :: (b -> c) -> (a -> b) -> a -> c ?

I think there's a typo in your signature. It should be:

    compose :: (b -> c) -> (a -> b) -> a -> c
Other than that, I agree!

Re: Stages of learning Go, with code examples

#73
post #14

Earlier quoted context omitted.

We prefer to use version control, and vendor libraries in our project instead. This is language-agnostic, and allows us to have repeatable builds without extra tools. I've written up a tutorial about it using git-subtree: https://github.com/jamesgraves/example-go-app

Does this not wholly preclude the possibility of vendors shipping a library without source?

We normally exclude the entire pkg directory, but in that case you could just add selected files from there.

Re: Stages of learning Go, with code examples

#74
post #21
post #5

Just wondering, does Go finally has a package manager with proper versioning of modules? Edit: Somebody is heavily downvoting me on every reply, I would love to know why. I like Go and want to get into it since a few months but the missing package manager held me off. So what is wrong about my question?

You really don't need a package manager for Go. Honestly, the main reason you need them for node, python, etc is because you're deploying source code to your production environment. You don't do that with Go. You deploy a binary, which has everything wrapped up in itself. Think of it as a self-contained virtualenv that requires zero effort on your part :) In Go, code is packaged via source control repos. If you want…

So the "solution" to Go's package management is just statically linking every library you use?

Re: Stages of learning Go, with code examples

#75
post #29
post #21

Earlier quoted context omitted.

You really don't need a package manager for Go. Honestly, the main reason you need them for node, python, etc is because you're deploying source code to your production environment. You don't do that with Go. You deploy a binary, which has everything wrapped up in itself. Think of it as a self-contained virtualenv that requires zero effort on your part :) In Go, code is packaged via source control repos. If you want…

> You really don't need a package manager for Go. Honestly, the main reason [...] is because you're deploying source code to your production environment Not sure if I would agree with that. A full-fledged package manager automates the process of installing, upgrading, configuring and removing software packages in a consistent manner. Dependency management and semantic versioning is an important part to provide this c…

>>A full-fledged package manager automates the process of installing, upgrading, configuring and removing software packages in a consistent manner.

That sounds like deb(dpgk)/rpm's job. In golang-land I just need to use vendoring+godeps for repeatable builds.

Re: Stages of learning Go, with code examples

#76
post #72

Earlier quoted context omitted.

Anything to do with higher-order functions, least of all compose :: (b -> c) -> (a -> b) -> a -> c ?

I think there's a typo in your signature. It should be: compose :: (b -> c) -> (a -> b) -> a -> c Other than that, I agree!

> I think there's a typo in your signature.

You think right, that'll teach me (once again) not to typecheck my comments. Thanks, and fixed.

Re: Stages of learning Go, with code examples

#77
post #25

Earlier quoted context omitted.

You probably were sarcastic, but in case you are not there's a brilliant article from Rob Pike about using and composing functions to create flexible APIs: http://commandcenter.blogspot.co.uk/2014/01/self-referential... There is countless proof about Go's composability through functions and interface, but I keep coming back to that one document.

There is countless proof about Go's composability through functions and interface I can't tell if you're serious or sarcastic yourself. The article proposes this snippet, presumably copy/pasted for every option: // Verbosity sets Foo's verbosity level to v. func Verbosity(v int) option { return func(f *Foo) option { previous := f.verbosity f.verbosity = v return Verbosity(previous) } } If that's what Go composability…

Unfortunately that is what I've experienced.

I direct your attention to sort: https://code.google.com/p/go/source/browse/src/sort/sort.go#... This defines sort on []int and []float64. If you want to sort a []int32, you have to manually define Int32Slice and copy paste lines 232-237. Copy paste the same lines again for any other slice of built-ins you wish to sort, e.g. uint32, uint64, int64, float32, etc.

They removed the entire set of vector containers now, but for the canonical way to implement IntVector, see https://code.google.com/p/go/source/browse/src/pkg/container... -- and compare that to stringvector.go in the same directory. It's almost entirely copy/paste with string substitution to replace int with string.

Re: Stages of learning Go, with code examples

#78
post #58
post #21

Earlier quoted context omitted.

You really don't need a package manager for Go. Honestly, the main reason you need them for node, python, etc is because you're deploying source code to your production environment. You don't do that with Go. You deploy a binary, which has everything wrapped up in itself. Think of it as a self-contained virtualenv that requires zero effort on your part :) In Go, code is packaged via source control repos. If you want…

An example for a dependency management package manager for a compiled language is [cocoa pods]( http://cocoapods.org ) for Objective-C. It's really popular with iOS and OS X developers. Or look at [cabal]( https://www.haskell.org/cabal/ ) for Haskell. It's a package manager that doubles as build system. It helps when you use external open source projects and want to update it to a new version. With go you have to kee…

Just to extend your list.

dub for D, http://code.dlang.org/

cargo for Rust, https://crates.io/

biicode for C and C++, https://www.biicode.com/

NuGet for C and C++, http://docs.nuget.org/consume/support-for-native-projects

OPAM for OCaml, https://opam.ocaml.org/

Re: Stages of learning Go, with code examples

#79
post #34

Earlier quoted context omitted.

The absence of generics makes it so that Go's composability is through copy/pasting your code while changing the types. There's even a tool to do the copy/pasting for you. Technically, it's composability, just not the best kind.

Generics and composability are orthogonal concepts, to me. I'm not really sure what sort of composability you're talking about. Can you provide an example for me to rip apart?

"Modularity" is nearly defined by the ability to forget certain details of an interface so as to leave freedom to change the underlying details while maintaining the behavior at the interface (Reynold's Abstraction Principle). In almost the same breath one would declare that types are a primary mechanism to achieve this. [0]

    Type structure is a syntactic discipline for
    enforcing levels of abstraction.
Modularity is more or less required in order to take a stab at defining or talking about composability, but is "good" modularity necessary (or sufficient) for achieving composability?

I don't have a bulletproof argument here, but essentially I'd like to say that greater modularity allows you to forget inessential details and subsequently increase the surface area with which you can compose your modules.

So finally, we come to generics as a powerful tool for using types to erase inessential details. Generics essentially operate via providing a new logical structure to types, the ability to introduce and operate sensibly over type variables. So I would argue that a type language without generics is much like a programming language without variables [1]. You are perhaps not completely crippled, but pretty close.

So, my argument is something like "without generics your type language is crippled so as to make control of modularity very weak which will prevent you (syntactically) from ensuring composability".

The "(syntactically)" bit is important since, as we all know, you can achieve modularity and composability "by hand" through code generation, copy and pasting, creating commenting policies... but then we've stumbled back into the "static types versus dynamic types" argument, so let's not rehash it.

[0] https://wiki.mpi-sws.org/star/paramore?action=AttachFile&do=...

[1] These kind of variables, not "mutable cell" style variables: https://existentialtype.wordpress.com/2012/02/01/words-matte...

Re: Stages of learning Go, with code examples

#80
post #57
post #26

Earlier quoted context omitted.

> presumably copy/pasted for every option To be fair, there is "go generate" now that can generate functions that would otherwise require tedious copy/pasting. If you are familiar with the preprocessor in C++ expanding template definitions, this tool can provide similar functionality but is more generalized. I think embedding [0] is the real big composability win in Go. [0] https://golang.org/doc/effective_go.html#em…

> "go generate" AKA macros. I wish they were at least intellectually honest and just call them "macros" and start nice supporting hygienic macros in the language.

Yes, I do see macros as a possible alternative to generics, but not sure if it would ever happen.
Post reply on HN