Go vs. Python
24 October 2014
11 comments
Python, Go
http://govspy.peterbe.com/
tl;dr; It's not a competition! I'm just comparing Go and Python. So I can learn Go.
So recently I've been trying to learn Go. It's a modern programming language that started at Google but has very little to do with Google except that some of its core contributors are staff at Google.
The true strength of Go is that it's succinct and minimalistic and fast. It's not a scripting language like Python or Ruby but lots of people write scripts with it. It's growing in popularity with systems people but web developers like me have started to pay attention too.
The best way to learn a language is to do something with it. Build something. However, I don't disagree with that but I just felt I needed to cover the basics first and instead of taking notes I decided to learn by comparing it to something I know well, Python. I did this a zillion years ago when I tried to learn ZPT by comparing it DTML which I already knew well.
My free time is very limited so I'm taking things by small careful baby steps. I read through An Introduction to Programming in Go by Caleb Doxey in a couple of afternoons and then I decided to spend a couple of minutes every day with each chapter and implement something from that book and compare it to how you'd do it in Python.
I also added some slightly more full examples, Markdownserver which was fun because it showed that a simple Go HTTP server that does something can be 10 times faster than the Python equivalent.
What I've learned
-
Go is very unforgiving but I kinda like it. It's like Python but with pyflakes switched on all the time.
-
Go is much more verbose than Python. It just takes so much more lines to say the same thing.
-
Goroutines are awesome. They're a million times easier to grok than Python's myriad of similar solutions.
-
In Python, the ability to write to a list and it automatically expanding at will is awesome.
-
Go doesn't have the concept of "truthy" which I already miss. I.e. in Python you can convert a list type to boolean and the language does this automatically by checking if the length of the list is 0.
-
Go gives you very few choices (e.g. there's only one type of loop and it's the
for
loop) but you often have a choice to pass a copy of an object or to pass a pointer. Those are different things but sometimes I feel like the computer could/should figure it out for me. -
I love the little
defer
thing which means I can put "things to do when you're done" right underneath the thing I'm doing. In Python you get thesetry: ...20 lines... finally: ...now it's over...
things. -
The coding style rules are very different but in Go it's a no brainer because you basically don't have any choices. I like that. You just have to remember to use gofmt.
-
Everything about Go and Go tools follow the strict UNIX pattern to not output anything unless things go bad. I like that.
-
godoc.org is awesome. If you ever wonder how a built in package works you can just type it in after
godoc.org
like this godoc.org/math for example. -
You don't have to compile your Go code to run it. You can simply type
go run mycode.go
it automatically compiles it and then runs it. And it's super fast. -
go get
can take a url likegithub.com/russross/blackfriday
and just install it. No PyPI equivalent. But it scares me to depend on peoples master branches in GitHub. What if master is very different when I go get something locally compared to when I run go get weeks/months later on the server?
Comments
For versioning dependencies check out gopkg.in
Thanks!
I had no idea about this. I haven't read it yet but skimming it lightly it seems like there *is* a solution.
Please find this program .. may be this will be the solution....These are related to map reduce ....
import MapReduce
import sys
"""
Word Count Example in the Simple Python MapReduce Framework
"""
mr = MapReduce.MapReduce()
# =============================
# Do not modify above this line
def mapper(record):
# key: document identifier
# value: document contents
trim_nucleotid = record[1][:-10]
mr.emit_intermediate(trim_nucleotid, 1 )
def reducer(trim_nucleotid, list_of_values):
# key: word
# value: list of occurrence counts
#mr.emit((person,len(list_of_values)) )
mr.emit(trim_nucleotid)
# Do not modify below this line
# =============================
if __name__ == '__main__':
inputdata = open(sys.argv[1])
mr.e xecute(inputdata, mapper, reducer)
What is this related to?
You might want to look into gopkg.in and/or vendoring when it comes to managing external dependencies.
Some package managers https://code.google.com/p/go-wiki/wiki/PackageManagementTools
Great article
That's amazing! That there are so many already.
Packaging a hard thing to get right. It's not hard like brain surgery but it's hard as like in carpentry.
I haven't tried Go yet so thanks for the summary of your experience.
I'm not convinced there's a strong use case for "defer" as I'd think if the block of code is large enough that a finally clause is "too far down" then it's already overly complex and using "defer" repeatedly throughout would make it worse. I could be convinced otherwise.
That said, you could easily make a Python context manager with similar semantics. You'd do "with deferred() as x:" and then could have x.defer(func, *args) throughout, and it would work in similar fashion at the end of the block. Might be useful some day.
Actually, I'm going to change my mind already. In some cases you have to set up a whole series of things, but in the finally clause you can't blindly tear down each of them. You need to check whether the code got to the setup (or raised an exception beforehand). The defer approach does seem like it would simplify that case as you could dispense with a bunch of conditionals in the finally clause. Hmmm... (goes to find code where this may be cleaner)
Yeah, a context manager and its __exit__ is maybe a good alternative to the `finally:` but it's a mess. I loooove Python but let's admit; Go wins this round :)
And godoc -http=:8080 is even cooler … simply open http://localhost:8080/pkg/, and you get the documentation for builtin packages like math, external like github.com/russross/blackfriday and $GOPATH/yourcoolstuff in one place, linked nicely :)
That's neat! That's basically what godoc.org is plus your own $GOPATH stuff.