Ask HN: Python or Go?
I have a strong background (8 years) in PHP development with Drupal and other misc frameworks/components. I'm a comaintainer for a system called Aegir, which is a free software control panel for managing large fleets of Drupal sites. Aegir is currently written in all PHP, including a daemon for running background tasks (which is not a good thing).
We're in the process of re-architecting Aegir to be a frontend + simplified API for Kubernetes or other prerolled container management systems (Flynn, Deis, etc). Our goal is to create a superset of what Aegir supports.
Anyway, we determined very quickly that PHP is not a very good language for the majority of what we're building. We've narrowed down the alternatives to Python or Go, and we're wondering if there's any strong, technical reason to go with one or the other for systems/container orchestration type work.
19 comments
[ 2.8 ms ] story [ 49.9 ms ] threadGo, being multi-threaded, and being designed to be usable by teams with a broad range of skill, eliminates the need to worry about asynch vs. synch code.
See this article explaining the problem with Async functions. "What Color is your function?" http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...
Quote from the article:—————————————————————————
Go is the language that does this most beautifully in my opinion. As soon as you do any IO operation, it just parks that goroutine and resumes any other ones that aren’t blocked on IO.
If you look at the IO operations in the standard library, they seem synchronous. In other words, they just do work and then return a result when they are done. But it’s not that they’re synchronous in the sense that it would mean in JavaScript. Other Go code can run while one of these operations is pending. It’s that Go has eliminated the distinction between synchronous and asynchronous code.
Concurrency in Go is a facet of how you choose to model your program, and not a color seared into each function in the standard library. This means all of the pain of the five rules I mentioned above is completely and totally eliminated.
So, the next time you start telling me about some new hot language and how awesome its concurrency story is because it has asynchronous APIs, now you’ll know why I start grinding my teeth. Because it means you’re right back to red functions and blue ones.
—————————————————————————
If you do not have to worry about asynchronicity, then this advantage may not mean as much to you, and Python's larger user base and libraries may be a compelling advantage. But if you do, not having your world of functions divided into two "colors" is a huge advantage.
Is there any way you can elaborate on your experience that brought you to this conclusion?
I am rather curious, as Go has the same middleware concept that django has to add in security, session, and token information to an http call.
On Golang:
1. There are a bunch of good frameworks out there for you to bootstrap with -- if you pick one realize its going to have shortcomings -- if you don't pick one and you think your going to use the standard library + tools like gorilla and alice, your going to end up doing a lot more work than you expect. -- a lot of the info on this front is stale, look for the context package on the google blog to see how the thinking is evolving (and its pretty rational)
2. There are parts of Go that look great on the surface, and work for "small programs" but if your building in the large they are going to be lacking. Logging is the example that comes to mind, and a lot of the "solutions" are just coming up short at the moment. Profiling on OSx being broken is another (and this isn't the fault of the GO, but rather apple.
3. Your going to have a very different workflow with go than you do with a python or PHP. This same would hold true if you moved from PHP to C or java. The distinction is that C and Java have a LARGE toolchain to support your transition, here again Go is lacking.
All that having been said, I would choose Golang over Python or PHP for what your trying to do. Your going to pay a large tax upfront (in both ramp up and real time development costs) for something that (potentially if you don't screw it up) is much more maintainable in the long run.
More generally, it becomes pretty clear when you're using a tool for something it's not really meant to do. PHP is an okay programming language/runtime, but there are other tools that would do some of these jobs better, hence our interest in Go and Python.
Recently, on a short term contract, I had to deal with a lot of PHP code that was running in a similar manner. I ended up using golang, to monitor the queue and spin up Goroutines that executed the PHP code ONCE. Between the concurrency, and php back in "one and done" mode the problem was "solved" and the internal team happily took over finishing the porting to pure Golang. Its a great example of Go "in the small" and it really does shine.
Go "in the large" is a whole other matter. You should read the following (in this order)
http://thenewstack.io/a-survey-of-5-go-web-frameworks/ <<< Im a user of gorilla/mux gorilla/context and alice for middleware chains. Gorilla/context and alice are not mentioned by this article, but the lament a the end about the concept of context being lacking in golang is spot on.
https://blog.golang.org/context <<< but this would be better for context however its does not mix with the current HTTP server (no ones solution does)... its from the go team but there isn't a lot of indication on how to use it and be future proof
https://joeshaw.org/net-context-and-http-handler/ <<< explains the context package well
https://github.com/remind101/pkg <<< has wrappers for getting net/context into gorilla and alice..
The end result is non of the GO web support is what I would call "great" yet. However it isn't stopping me from building with them. Look at the tradeoffs your going to have to make, be smart about how you couple your business logic to your server/framework, as your framework will quickly become a hard dependency. If your careful in that implementation, this http://blog.golang.org/introducing-gofix might be a way to back out one of those dependency if your unhappy.
The tooling to help you root out an issues exists and you should know its there and how to use it: http://blog.golang.org/profiling-go-programs http://saml.rilspace.org/profiling-and-creating-call-graphs-... https://deferpanic.com/blog/compile-time-code-weaving-in-go/
Golang is probably the better programming language. The main reason for this is its statically typed, and the type system is dead simple.
If I want rock solid code in Python, I have to write tests and isinstance checks everywhere. You begin to feel like making your reinventing the wheel just to get type safety.
Golang on the other hand won't let you compile of the code is wrong. In various ways the linter is more strict than pep8.
I also love how they kept struct. It leaves the impression your working with bare metal. The idioms for goosing, like Python, become intuitive and set in.
The issue that made be give up golang is the libraries are a joke. I'm talking dead simple things like orm and logging alone rule it out for anything serious.
You also begin to miss things like REPL, Python prompt toolkit, nummy, Ipython, etc. Django and sqlalchemy are things you'd kiss.
Golang is a better programming language, but it's way too green. You'd be reinventing the wheel constantly,
I would like to know what issues you had with it, as I'm in the middle of building out my own replacement logging package
Specifically, I wanted "logging" from python; leveled logging handlers, with colors (like colorama) and per-module level permissions.
> as I'm in the middle of building out my own replacement logging package
None of the alternatives did exactly what was needed. And this extended other modules as well. It turned out we would be maintaining forks of all these projects.
If you haven't already, https://github.com/avelino/awesome-go#logging is a good resource.
If the original poster / you do decide to go with golang I wish you the best. It's a great programming language - but you're time spent reinventing the wheel will outweigh golangs benefits over python.
And it's kind of a shame. The benefits of the static type checking, concurrency, compile times, testing, etc.
I can tell you, in every category - at the present moment - golang's available libraries fall short to what you get in python. Not golangs fault, it's just want maturity gives you.
Even things like asserts in python, (assert is even a builtin in python, no need to import unittest!) is non-existant in golang. It's not a "bad" thing.
My outlook for golang it strong - my concern is that you may drain away your runway doing what you should otherwise be doing on freetime.
Python is interpreted and deployment requires a large-ish runtime environment as well as making decisions about v2 vs v3. You can use virtualenv to address these and related issues.
Personally, I like Go, but keep ending up using Python and JavaScript far more often.
On the other hand, Go's general binary distribution is much simpler, running the compiler is like running lint on your code, concurrency is relatively easy.
At the moment, we're moving from a primarily Python environment to an environment to where we use Python for all user-facing web applications and Go for web service APIs. Your milage may vary.
The single binary feature of Go makes deployment really easy. And the standard library has everything you need to do an API. I chose the gin framework in my case with the standard html/template. The front end uses knockoutjs.
If you need some specialized library like machine learning or natural language processing, I would choose Python as there are many great libraries out there.
A good answer strongly depends on that. Here are some thoughts:
1) If it is for style and maintenance reasons, then Python with one of the frameworks (Flask is very easy to get started) would be a good start.
2) If performance is a reason, then see if you can separate out a portion of your PHP code to call the Go service. You can use message queues as a bridge between the services for better scalability.
For most applications pure Python will not hurt. Python is very productive to develop. If you need to scale in the future, you can identify bottlenecks and rewrite them.
tldr; Go is not Python, but it is still beautiful.