301 points · by A_Ghz · 12y ago · 124 comments
Guaranteed you'll find use for Go in one system or the other when you want easy deployment, fast development time and extreme speeds. :)
(Disclaimer: I love Go and I hope it goes mainstream in a big way in 5 years)
In a few other languages:
// C++
sort(vs.begin(), vs.end(),
[](const string& l, const string& r) -> bool {
return l.size() < r.size();
});
# Python
vs.sort(key=lambda s: len(s))
When they fix this, Go might be the perfect language for me. Until then, I'm not touching it with a pole.There's plenty of other ways in which Go is shorter than the equivalent C++ (e.g. with type inference, or channel operations), but no-one's trying to claim that the shortest code wins.
It's not just sorting, it's any parametric polymorphism: generic operations or non-built-in containers (not just in the sense of "array", an atomic ref is also a container) for instance. Go only lets do these in verbose, clunky ways, and losing much of the type safety Go is supposed to provide (what with being a statically typed language)
> no-one's trying to claim that the shortest code wins.
No one is indeed, so that's an irrelevant strawman.
sort(begin(vs), end(vs),
[](auto const& l, auto const& r) {
return l.size() < r.size();
});
Even simpler.dfrey already asked about why begin/end are functions now.
I see auto being used inside the lambda. Was that not allowed in C++11? I got sucked into the "use auto everywhere" mantra, so I just imagined it was possible, but could easily see if it were not. That is probably the biggest decrease in size to the original code posted.
I see return type inference. That is one thing I have heard about being new in C++14. To be honest, I would rather see the explicit return type there. Inference is always nice, though.
Can any "idiomatic" Go programmers here give a better example of how you would do this?
Sure.
sort.Sort(sort.Reverse(sort.StringSlice(vs)));
Instead of methods, you could of course write normal functions taking []string, but only methods can fulfil interfaces.
vs.sort(key=len) // Sort a slice of structs with first class functions.
type Album struct {
Title string
Year int
}
albums := []Album{
{"Born to Run", 1975},
{"WIESS", 1973},
{"Darkness", 1978},
{"Greetings", 1973},
}
less := func(a, b Album) bool { return a.Year < b.Year },
sorted := QuickSort(less, albums).([]Album)
For more fun things, see: http://godoc.org/github.com/BurntSushi/ty/fun L.sort(key=len)Making go-routines map to OS-level threads is a pain. And as much as I hate to trot out this old chestnut, the lack of generics is also a real pain.
I've gotten too used to having generic containers to go back, and the various hacks available just aren't worth it.
I still can't believe Go doesn't have some of the "basic" data types that older languages do such as Python's set() objects and the various operators.
That said, if was writing a small command-line utility for text manipulation or something else similar in scale that doesn't involve dealing with POSIX threading, I'd probably use it instead of Python, C, or C++.
> Library situation is a little iffy. Libraries are hardly iffy. Any C code be called, and most c++ code can be called very, very easily and with little boilerplate. The standard library has a huge pure section, and for almost everything else it has dozens of bindings to C libs. And there are plenty of existing libraries in the babel library manager, such as SFML.
Also, speaking only for myself, I really like the language and its ideas, but that one thing it's missing makes it too annoying to write code in it. I'd like to see more people aware of this deficiency and putting pressure on the language's owners to fix that problem, so that I can start using it.
> Go is that _slim_, and that's good! Guaranteed you'll find use for Go in one system or the other...
I'm all for learning new programming languages, but if you're going to learn a new programming language, why not use one that has new ideas?
Generics actually have very little to do with sorting a list in Go. The example that the parent gave could be expressed in one line as "sort.Sort(sort.Reverse(sort.StringSlice(vs)));" No generics needed-- only composition. sort.Reverse is a wrapper interface which can be composed with any other sort.Interface to reverse it. It is typesafe, too-- there are no typecasts.
There are cases where generics would allow us to avoid typecasts, but this is simply not one of them. In many cases, what you want can be expressed via composition of types, and often (in my opinion) in a clearer way than by using lambdas, continuations, and callbacks. I suggest learning a bit more about the language and keeping an open mind.
I also don't really have a lot of excitement about the prospect of solving problems by composing types in Go. For instance, to the best of my knowledge, Go does not have language support for sum types. It's not that I'd rather use continuations or callbacks -- I like powerful, expressive type systems to do, e.g., static enforcement of contracts -- it's just that I think that Go's type system isn't very expressive.
You're right, perhaps I'm totally misinformed about the language, and that lurking below there really is a way to do clear and clean things with types in Go. But everything I've seen points in the opposite direction, so at this point, though I have an open mind, I'd need more evidence to change my view.
Of course, being Rob Pike, decreeing that authority and experience is more valuable than any (well-reasoned) complaints from other people is a very convenient stance for him to take; how many complainers are going to rival his résumé?
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
How does forcing me to type out (or copy-paste) an implementation of Swap increase safety? If anything, it just makes the code more error-prone.And not having generics means that (as far as I can tell) people end up using a lot of `interface{}`. How does that increase safety?
More importantly, why would I want to use a language whose solution to the problem of "dealing with generic data" essentially boils down to "use a void*"?
Turbo Pascal 5.5 was compiling 34,000 lines/minute in 1989 in MS-DOS systems!
http://edn.embarcadero.com/article/20803
Compared with module based languages, C and C++ toolchains have been always slower.
It helped me build this: https://github.com/joewalnes/websocketd/
Here is a search for Go repositories with > 100 stars.[1] Every project will look different (the really big ones might be more complex then you need) but as you get deeper into the results you can find some great stuff.
[1]: https://github.com/search?l=go&q=stars%3A%3E100&s=stars&type...
- Eggs should not be washed.
Since your folder structure is also your package structure, with rmux[1] I tried to separate the project into dependencies, and use a package for each one. I like to also limit files to one class--but if you read through the default package files, the creators seems to go both ways.
One interesting thing that isn't mentioned often is how to handle the equivalent of #ifdef, through +build flags. This isn't the best solution, since all functions and their arguments are evaluated (even empty functions), but it does allow for the use of constants to wrap around cpu-wasteful debug-only areas/
[1]: http://fitzgen.github.io/pocco/ [2]: http://jashkenas.github.io/docco/
What about freenode/#go-nuts? Seems like a very nice channel to me.
- Eggs should not be washed.