AWS announced its support for Go as the language for AWS Lambda in January 2018. There were already some open source frameworks and libraries with which to shim Go applications that used Node.js (Apex serverless Framework), but now Go is officially supported and added to list of programming languages that you can use to write your Lambda Functions:
- Go
- Node.js
- Java
- Python
- .NET
But which language should we use to write efficient Lambda Functions? One of the reasons to go serverless is being a polygot. Regardless of the language you choose, there is a common pattern to writing code for a Lambda Function. Meanwhile, you need to pay extra attention to performance and cold starts. That's where Go comes into play. The following diagram highlights the main advantages of using Go for serverless applications in AWS Lambda:
- Cloud-oriented: It was designed by Google primarily for the cloud with scalability in mind, and to reduce the amount of build time. Go is a solid language for distributed systems and infrastructure tools. Docker, Kubernetes, Terraform, etcd, Prometheus, and many orchestration, provisioning, and monitoring tools are built using Go.
- Fast: Go complies into a single binary. Therefore, you provide a precompiled Go binary to AWS Lambda. AWS does not compile the Go source files for you and this has certain consequences, such as Fast cold-boot time. Lambda doesn't need to set up a runtime environment; Java, on the other hand, requires spinning up a JVM instance to make your function hot. Go has a clean syntax and clear language specifications. This delivers an easy language for developers to learn and shows good results quickly while producing maintainable code.
- Scalable: Go has built-in concurrency with goroutines instead of threads. They consume almost 2 Kb memory from the heap and work faster than threads; hence, you can spin up millions of goroutine at any time. For software development, there is no need for a framework; the Golang community has built many tools that are natively supported by Go's language core:
- Go's error-handling is elegant.
- Lightweight framework for unit testing.
- Solid standard library—HTTP protocol support out of the box.
- Common data type and structure supported—maps, array, structs, and so on.
- Efficient: It involves efficient execution and compilation. Go is a compiled language; it compiles into a single binary. It uses static linking to combine all dependencies and modules into one single binary file. Also, its faster compilation speed allows for rapid feedback. Speedy development saves time and money; thus, this is certainly the most significant advantage for someone with a tight budget. Moreover, it provides efficient memory utilization with garbage collector.
-
Growing community: The following screenshot shows the rising popularity and usage (as observed in the StackOverflow Survey 2017) for the most loved, dreaded, and wanted programming languages:
In addition, Go is backed by Google and has a large, growing ecosystem and numerous contributors to the language on GitHub, and great IDE support (IntelliJ, VSCode, Atom, GoGland) and debugging.