Switching from Python to Go? Here's What You Need To Know

Switching from one programming language to another can be a monumental step for any team, but when a new language offers benefits that outshine the one you're using, you may find it's time to make the move.

This year, the team at Stream, which offers an APITrack this API for building, scaling, and personalizing news and activity feeds, switched its primary programming language from Python to Go. Leaving Python behind was a big step for us, but using Go gives us advantages we couldn't ignore.

Go's Advantages

Performance:

Go is a very fast language for a dev team to use. Its performance is comparable to Java or C++, and we've found it's approximately 30 times faster than Python for any given use.

Language performance is important for us at Stream. While often the language is nothing more than the glue between an application and the database, customers often judge an API provider's efficiency by the language used.

Previously, we optimized tools like Cassandra, PostgreSQL, and Redis. However, we soon discovered their limits. For example, Python is a great language choice for many projects, but its performance can be sluggish when compared to others. This is evident in use cases like serialization or deserialization, ranking, and aggregation. With Python, we experienced performance issues that we could have avoided, such as instances where Cassandra would take 1ms to retrieve the data and Python would spend the next 10ms turning it into objects. The serialization/deserialization in Go is so fast that for our use cases it's typically below 1ms.

Go is also fast with regards to compile time. Stream's largest microservice written in Go takes six seconds to compile. Go's fast compile times are a major productivity win compared to languages like Java and C++, which are famous for sluggish compilation speed.

Productivity (and Not Getting Too Creative)

If you're new to Go, not much will surprise you when reading code.

Here is a Go code snippet from the very good How I Start Go tutorial, which showcases multiple assignments, data structures, pointers, formatting, and a built-in HTTP library:

func (w openWeatherMap) temperature(city string) (float64, error) {
    resp, err := http.Get("http://api.openweathermap.org/data/2.5/weather?APPID=YOUR_API_KEY&q=" + city)
    if err != nil {
        return 0, err
    }

    defer resp.Body.Close()

    var d struct {
        Main struct {
            Kelvin float64 `json:"temp"`
        } `json:"main"`
    }

    if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {
        return 0, err
    }

    log.Printf("openWeatherMap: %s: %.2f", city, d.Main.Kelvin)
    return d.Main.Kelvin, nil
}

Unlike Python, which provides advanced features that developers can use to flex their creativity when writing code, Go naturally makes developers adhere to the basics.

Thierry Schellenbach Author Stream-Framework (Python), Founder CEO getstream.io - Techstars NYC 2015. Enjoys Snowboarding, Surfing and Climbing.
 

Comments (0)