Golang’s most important feature is invisible

TLDR: The Go standard runtime allows for asynchronous coding while allowing the developer a simply synchronous interface.

I’ve worked professionally with Go for a couple of years and one thing that I have been repeatedly surprised about is how little attention has been given to what I consider Go’s most important feature.

When asked about Go’s most important features many folks will list Go’s simplicity, its C interoperability, compile speed, and concurrency. All of these are important features but what I consider the most important is almost invisible and can be quite subtle.

Let’s start with a code example showing the feature and see if you can guess it yourself?

Did you see it? There isn’t a lot of code there. To be honest the feature I’m talking about is demonstrated in this code, but since it is part of the standard Go runtime it is invisible. To better highlight the feature let’s look at an example of similar code written in Java.

Looks quite similar . . . except it’s not. Ignore that the code is longer, a good IDE will negate those tradeoffs. In order to make this code perform as well as the Go code you would need to do a LOT of extra work. Have you guessed it yet?

The feature I’m talking about is the Go runtimes handling of blocking goroutines. When I first started using Go, I saw goroutines as a more efficient variant on Java’s green threads but it never clicked that when I wrote code that looked like traditional blocking I/O that under the hood it was effectively more similar to async I/O. It is quite evident when doing blocking calls, Go is able to schedule work much more efficiently. To get this performance out of Java you would need to add threadpools, futures or some other async library.

But don’t take my word for it on performance, let’s run a quick performance test. These results were run using Apache Bench and run on the same machine with ample warmup time.

Apache Bench run against the Go Web server
Apache Bench run against the Java Web server

To make a long story short, the Java version hits ~21K requests per second while the Go version hits ~36K requests per second.

This is, of course, a synthetic example but is not far off from a pattern I use quite often. A network request comes in, it’s processed, routed to some code, that code does work, often another network or disk based request, and then the results are returned. In Go, all the blocking on network and disk is handled very efficiently. In Java and most other languages, you can get this benefit as well but it typically requires a lot more code, planning, and testing. It’s virtually free in Go. That is very powerful.

In languages like C you might use a library like libevent to get this type of behavior. In Java you might use threadpools and futures. In python you would rely on async/await. But in Go you get it for free, you get to write your code as if it’s synchronous and at any point blocking occurs the goroutine can be suspended and other goroutines that are ready to do work can be scheduled.

So, as a server engineer who does a lot of this pattern of work, I think it’s Go’s strongest and often misunderstood feature!

What do you think?

Note: This article have been indexed to our site. We do not claim legitimacy, ownership or copyright of any of the content above. To see the article at original source Click Here

Related Posts
"One Arrow 540 Stars"! SpaceX's ten-handed Falcon 9 rocket works wonders thumbnail

“One Arrow 540 Stars”! SpaceX's ten-handed Falcon 9 rocket works wonders

北京时间1月13日23点25分,SpaceX在美国卡纳维拉尔角40号发射台成功执行了“Transporter3”发射任务,亮点多多,意义重大。 首先,执行这次发射任务的猎鹰9火箭编号B1058.10,这也是它的第十次飞行,而且再次回收成功。它也成为B1049、B1051之后,SpaceX的第三枚“十手火箭”。这次采用了陆地回收的方式,回收平台距离发射台只有几百米。这枚火箭第一次执行任务是2020年5月30日,仅仅用了19个月就完成了10次发射和回收,创下新纪录。第二,Transporter3是一次“太空拼车”任务,一共搭载了105颗来自世界各地的小卫星。其中最大的是地球成像卫星乌克兰Sich 2-1,重量170公斤,芬兰ICEYE、美国Capella/Umbra三家公司的五颗SAR雷达遥感卫星,44颗小型“超级鸽”光学成像卫星。2021年1月的Transporter 1发射了143颗小型卫星,6月Transporter 2则发射了88个小型卫星,三次任务合计达336颗。这种太空批车任务中,SpaceX的收费是每公斤5000美元,相当相当低廉了,也再次展示了可重复火箭的优势。第三,加上这次任务,B1058火箭已经累计发射了大约540颗卫星,总重量超过120吨。之前它曾经发射过大约290颗星链卫星,并为国际空间站发射过一次载人龙飞船任务、两次龙飞船补给任务,还执行过一次韩国地球静止轨道卫星的商业发射任务。
Read More
AirTags und Stalking: Apple ergänzt Handbuch für persönliche Sicherheit thumbnail

AirTags und Stalking: Apple ergänzt Handbuch für persönliche Sicherheit

Apple hat sein umfangreiches "Handbuch für persönliche Sicherheit" um AirTags erweitert. Das Dokument erklärt Nutzerinnen und Nutzern im Detail, wie sich iPhones & Co etwa vor einem misstrauischen Partner absichern lassen – und mit Apples Diensten durchgeführte Überwachungsversuche durch vertraute Personen erkannt werden können. Fremde AirTags aufspüren Neu ist das Kapitel zum Thema "Mit AirTag…
Read More

Apple thinks Android phones are not worth one (and some of its devices)

Για χρόνια, η Apple επιτρέπει στους πελάτες της, σε ορισμένες περιοχές, να ανταλλάσσουν τις παλιές τους συσκευές για να έχουν έκπτωση όταν αγοράζουν ένα νέο προϊόν. Αυτό επιτρέπει στους καταναλωτές να επωφεληθούν από αξιοπρεπείς εκπτώσεις, ανάλογα με την κατάσταση και την ηλικία της συσκευής τους.Η εταιρεία δεν περιορίζει το trade-in πρόγραμμα μόνο στα προϊόντα της…
Read More
OnePlus Nord CE 2 5G, análisis: uno más en la gama media thumbnail

OnePlus Nord CE 2 5G, análisis: uno más en la gama media

Compartir Probamos el último smartphone asequible de OnePlus, un gama media de 359 euros que quiere competir en el mercado más disputado. Análisis a fondo del OnePlus Nord CE 2 5G.Cuando OnePlus decidió regresar al segmento de la gama media con el OnePlus Nord, la mayor parte del público quedó convencida. Al fin y al…
Read More
News from Japan in the 39th week thumbnail

News from Japan in the 39th week

Dnešní přehled je velký. Ne, je VELKÝ a DLOUHÝ a může za to samozřejmě probíhající Tokyo Game Show. Sice je pouze online, ale to nebrání vydavatelům a studiím, aby představily svou dosavadní práci a případné výhledy do budoucna. Nejdřív na vás čeká hejno krátkých zpráv. Nejnovější vydání Weekly Famitsu potvrdilo informaci, že Earth Defense Force…
Read More
Index Of News
Total
0
Share