Google’s Golang — The Perfect Solution to Super-fast Building
From the large variety of programming languages, you can always find the right one that will be the most helpful for solving some specific task you come upon with.
In this article, you will learn about the basics of the Go programming language, which is primarily developed at Google. Google Go is a simple and easy-to-learn programming language that has many innovations that are missing in other languages.
“Go, also commonly referred to as golang, is an open-source programming language that makes it easy to build simple, reliable, and efficient software.”
If you would like to know more, ‘A Tour of Go’ is a typical starting place and includes many examples of syntax and language features, but Go by example is also a popular and is a useful resource. The easiest way to start writing Go straightaway is to use the playground where you can experiment (it’s also inbuilt into the tour of Go).
History:
Go is a Programming Language designed by Google Engineers Robert Griesemer (Hotspot and JVM), Rob Pike (UNIX & UTF-8), and Ken Thompson (B, C, UTF-8).
Go was conceived in 2007 to improve programming productivity at Google, in an era of Multicore processors, computer networks, and large codebases.
Go was officially announced in November 2009. It is used in some of Google’s production systems as well as other firms.
Why Golang?
In the beginning, making the transition to Golang might feel strange as your code gets longer, and you have to handle each error, but soon your code becomes more readable, simple, and stable.
Another reason for making the transition is the professional opportunities available through the language itself. According to the developer’s result survey in 2019, Golang has proven to be one of the highest-paid programming languages.
Here is the main four reasons to start with Golang:
1. Performance: Speed, Go enhances the availability and reliability of services.
2. Reliability: Golang is advanced and reliable, offering great built-in ways to handle errors. Go increases code readability through its simplicity.
3. Productivity: Developers can easily learn and adapt to Golang and quickly become productive.
4. Efficiency: It is efficient, compiling down to one binary.
Features of Golang:
Binaries: Go generates binaries for our applications with all the dependencies built-in. This removes the need for us to install runtimes that are necessary for running our application. This eases the task of deploying applications and providing necessary updates across thousands of installations. With its support for multiple OS and processor architectures, this is a big win for the language.
Language Design: The designers of the language made a conscious decision to keep the language simple and easy to understand. The entire specification is in a small number of pages and some interesting design decisions were made via Object-Oriented support in the language, which keeps the features limited. Towards this, the language is opinionated and recommends an idiomatic way of achieving things. It prefers Composition over Inheritance and its Type System is elegant and allows for behavior to be added without tight coupling the components too much. In Go Language, “Do More with Less” is the mantra.
Powerful standard library: Go comes with a powerful standard library, distributed as packages. This library caters to most components, libraries that developers have come to expect from 3rd party packages when it comes to other languages.
Package Management: Go combines modern-day developer workflow of working with Open Source projects and includes that in the way it manages external packages. Support is provided directly in the tooling to get external packages and publish your own packages in a set of easy commands.
Static Typing: Go is a statically typed language and the compiler works hard to ensure that the code is not just able to compile correctly but other type conversions and compatibility are taken care of. This can avoid the problems that one faces in dynamically typed languages, where you discover the issues only when the code is executed.
Concurrency Support: One area where the language shines is its first-class support for Concurrency in the language itself. If we have programming concurrency in other languages, we can understand that it is quite complex to do so. Go Concurrency primitives via go routines and channels make concurrent programming easy. Its ability to take advantage of multi-core processor architectures and efficient memory is one of the reasons while Go code is today running some of the most heavily used applications that are able to scale.
Testing Support: Go Language brings Unit Testing right into the language itself. It provides a simple mechanism to write our unit tests in parallel with our code. The tooling also provides support to understand code coverage by our tests, benchmarking tests, and writing example code that is used in generating our code documentation.
Advantages of Golang:
The Go programming language has seen explosive growth in usage in recent years. It seems like every start-up is using it for their backend systems. There are many reasons that developers find it attractive.
Go Is Fast: Go is a really fast language. Because Go is compiled to machine code, it will naturally outperform languages that are interpreted or have virtual runtimes. Go programs also compile extremely fast, and the resulting binary is very small.
Easy to Learn: Go’s syntax is small compared to other languages, and it’s easy to learn. We can fit most of it in our heads, which means we don’t need to spend a lot of time looking things up. It’s also very clean and easy to read. Non-Go programmers, especially those used to a C-style syntax, can read a Go program and usually understand what’s going on.
Static Typing: Go is a strongly, statically typed language. There are primitive types like int, byte, and string. There are also structs. Like any strongly typed language, the type system allows the compiler helps catch entire classes of bugs. Go also has built-in types for lists and maps, and they are easy to use.
Interface Types: Go has interfaces, and any struct can satisfy an interface simply by implementing its methods. This allows us to decouple the dependencies in our code. We can then mock our dependencies in tests. By using interfaces, we can write more modular, testable code. Go also has first-class functions, which open up the possibility to write our code in a more functional style.
Standard Library: Go has a nice standard library. It provides handy built-in functions for working with primitive types. There are packages that make it easy to stand up a web server, handle I/O, work with cryptography, and manipulate raw bytes. JSON serialization and deserialization provided by the standard library is trivial. With the use of “tags”, you can specify JSON field names right next to struct fields.
Testing Support: Testing support is built into the standard library. There is no need for an extra dependency. If we have a file called thing.go, write our tests in another file called thing_test.go, and run “go test”. Go will execute these tests fast.
Static Analysis Tools: Go static analysis tools are numerous and robust. One, in particular, is gofmt, which formats our code according to Go’s suggested style. This can normalize a lot of opinions on a project and free our team to focus on what the code is doing. We run gofmt, golint, and vet on every build, and if any warnings are found, the build fails.
Garbage Collection: Memory management in Go was intentionally made easier than in C and C++. Dynamically allocated objects are garbage collected. Go makes using pointers much safer because it doesn’t allow pointer arithmetic. It also gives us the option of using value types.
Easier Concurrency Model: While concurrent programming is never easy, Go makes it easier than in other languages. It is almost trivial to create a lightweight thread, called a “goroutine”, and communicate with it via a “channel.” More complex patterns are possible.
Disadvantages of Golang:
As we discussed above, Go is a good language. It has a clean syntax. It has fast execution times. There are plenty of good things about it. However, a programming language is more than its syntax. Here are some things that we ran into.
No Generics: First, the elephant in the room. Go doesn’t have generics. This is a big hurdle to get over when coming from languages like Java. It means a decreased level of reuse in our code. While Go has first-class functions, if we write functions like “map”, “reduce”, or “filter” that operates on a collection of one type, we can’t reuse those same functions for a collection of a different type. There are ways to deal with this, but they all ultimately involve writing more code. This hurts productivity and maintainability.
Interfaces Are Implicit: While having interfaces is great, structs implement interfaces implicitly, not explicitly. This is stated as strength of Go, but we found that it’s difficult to tell from looking at a struct whether or not it implements an interface. We can only really know by attempting to compile the program. This is fine if the program is small, but not if it’s a medium to large size.
Poor Library Support: Library support for Go is spotty. Our API integrates with Contentful, which does not have an officially supported Go SDK. This meant we had to write (and maintain!) a TON of code to request and parse data from Contentful. We also had to rely on a third-party Elasticsearch library. The Go SDKs that are provided by vendors don’t get as much love as their Java, Ruby, or JavaScript counterparts.
Difficult Community: The Go community can be non-receptive to suggestions. Consider this issue in the GitHub repository for golint: https://github.com/golang/lint/issues/65. Someone requested the ability for golint to fail a build when there are warnings found (which is something we’re doing on our project!). The maintainer immediately dismissed the idea. Enough people commented on the issue, and the maintainer eventually added the requested feature, over a year later.
The Go community also appears to have an aversion to web frameworks. While Go’s HTTP library covers a lot, it doesn’t provide support for path parameters, input sanitization, and validation, or many cross-cutting concerns often found in a web application. Ruby developers have Rails, Java developers have Spring MVC, and Python developers have Django. But many Go developers choose to avoid frameworks. Yet there are still frameworks out there. A lot of them! But it’s nearly impossible to choose one that won’t be abandoned after you’ve started a project with it.
Fractured Dependency Management: For a long time Go did not have a stable, official package manager. After years of begging from the community, the Go project has recently released godep. Before that, many tools filled the gap. We use the very capable govendor in our project. But this means the community is fractured, and it can be extremely confusing for developers new to Go. Also, virtually all package management is backed by Git repositories, the history of which could change at any time. Compare this to something like Maven Central, which will never delete or change a library that your project depends on.
Where?
Google Discover 7 biggest companies using Golang for their products — Google. Golang was designed by Google engineers and is often used there for internal projects. Twitch Discover 7 biggest companies using Golang for their products — one of the Golang examples is Twitch. It is appreciated for its simplicity, security, efficiency, and readability, which means that it perfectly manages problems encountered when displaying live video and simultaneous chats of a large number of users. Dropbox, one of the leaders in cloud computing services constitutes another great example of a major company using Golang. This allowed them to improve caching, improve the standard error interface, enable programmers to generate SQL statements programmatically, and implement fully functional Memcache client libraries.