What’s so great about Go?
To paraphrase the indie band Cracker, what the world needs now is another programming language like I need a hole in the head. That said, Go has slowly but surely inundated the development world like a creeping vine, covering everything that came before it in a lush—and in many ways superior—cover of programming power. With its growing popularity, there are a number of reasons you should take a closer look at it.
The story goes that Google engineers designed Go while waiting for other programs to compile. Their frustration at their toolset forced them to rethink system programming from the ground up, creating a lean, mean, and compiled solution that allows for massive multithreading, concurrency, and performance under pressure.
Running Go on modern hardware—and even inside containers or on virtual machines—can be a real pleasure. Because Go was designed to run on multiple cores, it is built to support concurrency and scale as cores are added. Further, its multithreading capabilities—most specifically, its goroutines—are a surprising and welcome addition to your bag of tricks.
Built for concurrency
Before we dig in further, let’s look at goroutines. These are essentially concurrent functions that run while the rest of the program completes. In other words, you can fire off a goroutine, have it run, and keep going while it and thousands of its brethren process in the background. Network timeout? Don’t worry, your goroutine will manage it while the main loop continues. Complete database failure? Your goroutine will know, and you can gracefully work around the issue.
Take a look at this simple example:
package main
import (
"fmt"
"time"
)
func hello() {
fmt.Println("Hello world goroutine")
}
func main() {
go hello()
time.Sleep(1 * time.Second)
fmt.Println("main function")
}
The main
function works just like it does in C. In this code, main
fires off a goroutine—indicated by the go
keyword—called hello()
. The program takes a quick nap and then continues. If anything were to happen in the hello()
function, the main
function wouldn’t notice. All that’s important is the work is done.
The program finishes when the main
routine wakes up and fires off a Println
. The hello()
function could be anything—a data call, a transaction, a queue entry—and it would run while the rest of the program churns along. Like the aforementioned vine, think of goroutines as little flowers that branch off and die while the main vine keeps going.
Want to see it in action? Check out this snippet at the Go Playground and you’ll see just how goroutines work.
This feature alone is worth the price of admission. For web apps, goroutines let you run concurrent jobs while avoiding roadblocks. If you’re waiting for data input, for example, a goroutine can fire off to supply pre-populated text even as the user is typing. In other words, your program will keep running even if the concurrent processes take longer than expected.
Best of all, goroutines are cheap, making Go fast.
“The Goroutines are multiplexed to fewer number of OS threads,” wrote Naveen Ramanathan on GoLangBot, a Go learning resource. “There might be only one thread in a program with thousands of Goroutines. If any Goroutine in that thread blocks, say, waiting for user input, then another OS thread is created and the remaining Goroutines are moved to the new OS thread. All these are taken care of by the runtime and we, as programmers, are abstracted from these intricate details and are given a clean API to work with concurrency.”
Why should you learn Go?
You’ve probably seen some tweets or blogs in recent years arguing that Go is better than Perl/Python/C/etc. We’ve all been using something—heck, I even use command line PHP—to do little things on our servers. Want to watch a log? Perl. Want to send out regular tweets? Python. Want to manage incoming data from thousands of concurrent users? Java or C++ was always your answer. So is Go “better”?
Well, Go, can do all of that. Go ensures that your toolbox is compilable across all platforms and on all hardware. It uses a surprisingly simple package management solution that “just works” and it is extremely portable. You can easily replace your scripting languages with Go and your compiled languages will definitely get a run for their money when stacked against Go solutions.
Go is built for software engineering today. Not everything new is great, but when a programming language is designed for exactly the environment most of us use right now—scalable, cloud-based servers that are optimized for performance—a lot can go right. Go is compilable on nearly any machine, so you can use it to create a full webapp or a tool to clean up incoming data for processing. Like Perl before it, Go is a Swiss Army knife, but one that has stripped off all of the overhead and extra junk that has accreted onto programming platforms over the past few decades.
Go is simple. As a dev, I’ve been able to pick up languages and frameworks over a long weekend. This gives me an understanding of a few important platforms and, since I don’t have to code daily, enough rope to hang myself when it comes to creating little side projects. Go has been different. For folks who already know the basics of programming or a few other languages, learning Go takes a few hours at most. Once you know its tricks, you’re ready to code. Again, because it is so fast, you’ll want to use it for nearly everything you used command line interpreters for, thereby replacing your bash scripts, Python sketches, and Java efforts.
Go is fast. Take a look at this clever to-do list in Go by Mohamad Fadhil. The code is quite simple—set up a MySQL database in Docker, add in a bit of HTML, and almost instantly you have a super fast web app that compiles at the command line. What’s more, Fadhil explores why he likes Go in the first place: speed.
In a regex test—regex-redux —Go ran in 3.55 seconds while Java ran in 5.58. The Go program weighed in at 102 lines of code, while the Java program weighed in at 70. True, Go was a little heftier than Java, but given the speed at which Go bested the next most popular system language, it’s clear you’re not sacrificing speed.
Go is on course to take over many open source and private projects and as it becomes more useful as a standard language for both microservices and the web. It could start replacing less performant solutions like Java and C.
Don’t take my word for it. Check out GoLang.org for advice on writing web applications, command line tools, and services.
“Personally, I believe Go is the new Java. Many open-source projects (e.g., Jaeger, Kubernetes, Docker, InfluxDB, Serf, etc.) are now written in Go. In contrast, the older projects (e.g., Apache Kafka, Apache Hadoop, Apache Spark, etc.) are written in Java,” wrote Fadhil.
Go is like chess or, well, the game of Go: it takes a moment to learn and a lifetime to master. Luckily, unlike chess, Go’s difficulty goes down with experience and soon you’ll be coding fast and furious programs in one of the world’s most modern languages.
Tags: go, golang
47 Comments
I do hope there is indentation in the real code? I honestly can’t read the sample code here, it’s a mess with 0 indentation.
I also like crystal (similar niche). But then I’m a fanboi 🙂
How is a benchmark where Python, C# and even PHP all beat Go by a significant margin proving that Go is fast?
Also, it seems like many of the fast languages just invoke the PCRE library. And Go is is among the worse languages of those that do. Java might be slow in this benchmark, because it uses its built-in regex library (possibly because interop is notoriously bad in Java).
Also, sorry, but I wouldn’t call a language that still doesn’t have generics in 2020 “modern”.
They aren’t saying Go is the fastest option, but simple that Go is faster than Java. A “duh” moment for anyone familiar with Java. A stack of cocktail napkins is usually faster than most things done in Java. In my experience, Java is one of the Devils we know. (flash being another) The JIT runtime comes at a high cost (your app isn’t fully compiled until it’s running), memory management is a horrible joke, and the promise of cross platform independence has always been a lie. That’s partly a lie with Go as well — your Go application will “work” anywhere the golang compiler, runtime, AND modules have been ported. (Is there a Go for BeOS? SCO? AIX? HP-UX? Tru64? VMS/MVS? …)
It’s an interesting “new shiny”. However, I think much of the buzz can be summed up in a single word: Google.
Go also implement their own regex.
Go does not need generics. It already has its empty interface and type switching.
One might end up writing more code, but, in exchange for that it makes the code more readable comparatively.
Also, it is the only language, which is not too demanding of IDE features to be usable.
The language is called “Go”, not “GoLang”. “Golang” is just used in search engines to specify to the search tool that we are looking for results about “Go” the language and not “go” the dictionary word.
Go is not learnt in a few hours. I’m a contractor and learn fast. I did a 9 month project in Go last year. By the end of it I could see some benefits in Go. The main one I saw was how the language constraints help avoid complex models. What you get used to however, is just how much copy and paste and boilerplate exist in a Go project. It was hard to get used to stateful local variables, which experienced programmers know are a source of bugs, Go forces their use while in Java for example you can effectively use final local variables.
Go is verbose, but what I found at the end of 9 months of Go full time was that you start to read go in large stanzas and not line by line. There are very common ways of writing code that while verbose they become so common that your eye can read them quickly and spot oddities.
At the start however this is very uncomfortable. I also became used to copy and pasting without feeling bad, Go just encourages it. I would never write code like that in another language.
Overall go is good IMO but the packages, go routines and advanced language features are not as simple to learn and use effectively as you make out.
Sincerely
When people start put into comparison speed tests and say it is faster I start to think they have no other arguments. Long time ago I had experience to write programs in Assembler language. I can say it will be faster then GO, do I wanna use it? No. Will I ever would like to GO? No.
Why on earth I would like to GO, if I can fly with functional languages?
When I see GO code I wanna cry. It is the worst code ever. I do not understand GO popularity.
The main problem is that the code is very difficult to read. The error handling is hiding logic. When I see a methods implemented in GO they full of noise of checking errors. Those checks hide the valuable logic.
Just watch this https://vimeo.com/97344498
Go is just Go, not Golang, not GoLang
Nothing is great about Go. It is yet another low-level, pointer-filled, error-prone language. No competent software engineer would use such a language.
that’s so true, they should use Rust, Scala, Erlang, etc.
This part made me cringe: “It could start replacing less performant solutions like Java and C”. I can’t see how you can replace well written C. The language is fundamentally made to allow for maximum control which will always (albeit with more work) yield more performance than a managed language.
Not only more work, but more bugs. One of the primary developers of GO was a Brian Kernighan, who also happend to be one of the inventors of the C programming language. After all these years, he took to the opportunity to learn from mistakes to create a new language that leverages the lessons learned.
Repeat after me, Go is not a replacement for C. It never will be. Yeah Go, eliminates a lot of bugs by using a garbage collector. Do you think creators of systems programming languages like C/C++, Rust etc. were stupid since they don’t do the same? Only an idiot would use Go for performance heavy projects like OSes or game engines. So I don’t understand why you say that Kernighan made go after learning from his mistakes in C.
> One of the primary developers of GO was a Brian Kernighan
False.
> who also happend to be one of the inventors of the C programming language
Also false.
Do you bother to verify anything you say or there simply no filter ?
The only way to compare performance with java is to run it several times in a single startup. There’s overhead for starting up a virtual machine. Furthermore the JIT optimizes the code on the fly through each run until it gets marked as no longer optimizable. Plus, the test you used is not even going to be the same algorithm every time. So you are essentially comparing regular expression implementations, and without the multiple run thing I mentioned, making it apples and oranges.
You should know that GO also runs under a virtual machine, with garbage collection, etc. So it is entirely to compare to include the vm initialization costs when comparing timing and resources.
not true, src https://golang.org/doc/faq#runtime
quote
“ It is important to understand, however, that Go’s runtime does not include a virtual machine, such as is provided by the Java runtime. Go programs are compiled ahead of time to native machine code (or JavaScript or WebAssembly, for some variant implementations). Thus, although the term is often used to describe the virtual environment in which a program runs, in Go the word “runtime” is just the name given to the library providing critical language services.”
Apache Spark is not written in Java. It is written in Scala. Yes, it runs on the JVM, but it is still a different language.
All the JVM projects mentioned are data centric – Kafka, Hadoop, Spark are all about big data. All the go projects except InfluxDB are pertaining to microservices/containerization.
Maybe the way to see this isn’t past vs future, but rather big data languages vs. microservice languages.
Misleading Article from just another go fanboy, comparing things which fail in real world. Please dig deep in c or java then compare 🙂
I like Go, but your statement about regex-redux speed is a bit misleading. Using pure Go (the built-in regex library), it’s quite a bit slower that the speed shown. I submitted a pure-go program for regex-redux. That high-speed version calls into a highly optimized C regex library, “pcre”.
Unfortunately, almost all of the points made here have either been contradicted, or are controversial to say the least. This would be more believable if the article was a bit more balanced. Go has its fair share of weirdness and syntax quirks, there would be at least this much to say why it’s not honey and peanuts at all.
Nice. Similar article about Rust is https://stackoverflow.blog/2020/01/20/what-is-rust-and-why-is-it-so-popular/
i’d like to love go, but the syntax of anything that goes beyond “hello world” is just ugly as hell when coming from a C-style background (C, C++, C#, Java, JavaScript etc.). simply unreadable.
I’ve really enjoyed learning Go. Really simple to adobpt. Things like excluding generics was by design to keep the language simple. And it’s verboisty is initally annoying but also why it’s commpeting at an enterpise level. The verbosity adds clarity for future maintainers of unkown skill level. Working on Ruby or PHP projects where previous developers have tried to be “clever” making everything magic, you have a hard time writing obfuscated code in Go. I learnt Nim first (which I really fond of) but Go won me over with the established eco system.
🤣🤣🤣🤣🤣
What’s so great about Go?
Obviously not that much …
Rust, Go, Javascript for winners
C++ for fast sadists
Python for happy sloths
Java for unimaginative enterprise systems that are surprisingly poor in design and performance
PHP and Ruby for children making toys
Fun fact: the idiom “like I need a hole in the head”, which my mother used to use (and I’m an old man already), predates the band Cracker by quite a long time.
https://www.grammarphobia.com/blog/2012/10/hole-in-the-head.html
You have to work pretty hard to generate a crash in our application due to a null pointer or writing to a memory location that you have previously feed.
A large number of common programming errors that cause such conditions are caught at compile time.
No competent software engineer responsible for ensuring that their software is reliable and maintanable wouldn’t, at the very least, investigate GO to see if it was appropriate for their application.
For God’s sake, please never use time.Sleep to synchronise goroutines. There are chan and the sync module in golang for that.
Does package create a namespace for the while file, or just until a new package statement? Seems somewhat C and python like. I like the thread concurrency management, but what if all threads refuse to yield or do i/o, does each get a lwp? So many questions!
As far as I remember, all dependencies are expected to be available as source code. That’s a serious problem.
No, why?
Yes? go dep downloads dependencies from the SOURCE REPOSITORY 🙁
Wow, what a lot of “party poopers” just hawking around Internet blog pages with a lot of pre-written invectives ready to put something down they don’t like.
Let me come at it from a techie but non-developer standpoint. I’m not a trained developer, most of work for the last 30 years has been coding in scripted languages apart from my love of Java. Got in to Golang about a year ago and much like Java, I like the strong typed ideals and I like the verbosity. I learned C many moons ago, even some Z80 assembler, so I have some experience of “tight languages” but there’s something about Golang that doesn’t allow me to shoot myself in the foot working on the basic coding requirements I have most of the time.
As a non-dev I’m never going to code ultra-critical enterprise class apps and tools, I’m simply coding automated processes with small and limited requirements, so I need language that for my basic needs is looking out for my stupid mistakes. You can get all smug and say “If you’re not prepared to code in assembler, C or C++ then you don’t deserve to code anything!”, well I’m sorry but I don’t want to be a developer I simply need to write some small utils and tools and I happen to like Golang, it suits we non-devs while trying to help is not make utterly stupid mistakes like poor memory management to wasting resources.
When I need to move files around I use Powershell, if I need something on Windows to do a little more then I code in C# but when I need to be able to code across OSX, Linux and Windows with a single tool/util then Golanf is “good enough”, not stuning, certainly not perfect but good enough for we non-devs.
Every language has pros and cons. I read this blog post to have something to convince me to learn Go. But the bias in the article drove me off completely. “It could start replacing less performant solutions like Java and C”. Less performant solution like C? What?
By the way, the blog post does not mention the low memory footprint and fast startup time of Go applications inside containers. These properties really impressed me about Go.
I started learning Go but then heard that generics will be introduced and few more changes. I stopped learning it.
Seeing all, i guess Go is going Java way. They want to do everything on the earth.
You might be happy to hear that you are wrong about Go going the Java way. Since Go 1.0, the Go team made it clear that the focus is on keeping the language small and stable. After all, they created Go after watching how C++, being already a feature monster back then, got loaded with another 35 features for its next version. So the Go team decided that Go shall not become a feature behemoth like C++ or Java. Generics are an exception to the rule, and it took years (!) to develop a generics design that fits into Go’s minimalist language philosophy.
The arrogance and ignorance by a few developers here is astonishing.
Accept what Go is, where it has its strengths and weaknesses and use it accordingly for the job where it is the right fit.
Go is a nice tool for some jobs, embrace it if you enjoy learning new programming languages,
but stop crying that a certain programming language is so much better, such a debate is pathetic.
You Sir, just said what I felt reading these replies. Thank you.
Hats off!
“use what you want to use, every tool has its problem it can solve”
except it’s false
Some languages are more error prone, less readable, more painful to use, etc. than others.
Go is a “meh” tool IMHO: better than old C, but nothing particularly interesting not great.
Go seems to be just a restricted language driven by a Google hype, not a tool that will really help programmers. (and yes I have used Go in projects).
Concurrency can be notoriously difficult to get right, but fortunately, the Go open source programming language makes working with concurrency tractable and even easy. If you’re a developer familiar with Go, this practical book demonstrates best practices and patterns to help you incorporate concurrency into your systems.
https://funkyprogrammer.uk/concurrency-in-go-programming-language/
That’s the beautiful way of explaining with examples related to living beings. Great explanation of Go routines. I liked it pretty much.
sooo many programmers calling it go when in fact it is google…… why everyone is calling it go??? google.com just confirm yourself smh …… and go[oogle] is still not better then yahoo or duck duck goose……