What is Golang? Where and Why is it used?
Go (or Golang), is an Open Source programming language developed by Google. Go has many use cases like used in building Microservices, Cloud Computing, Web Applications and is used in creating CLI Tools.
Go is a statically typed language and its structures and syntaxes resembles the C Programming Language. Due to Golang's fast startup time, low runtime overhead and the language being platform-independent, it has become very popular language for writing microservices and other use cases. Go is also popular for its concurrency, meaning that it can execute multiple processes at the same time.
Golang uses Goroutines (lighweight processes) and a collection of packages and commands for dependancy management. It was designed to solve problems like slow build times and difficulty writing tools and for cross-language development.
In this blog, I'll be discussing about the Golang and resources to get started with Golang.
History of Go
Golang was created by Google to solve their own software engineering issues and was created as an alternative to the infamous C++. Google started creating Go in 2007, and their goal was to create a programming language that was easier to use and have the useful features of other programming language like C++, Javascript and Python. It was released as a Open Source project by Google in 2009 where people could contribute to the development of Go.
Since then, many new features are being added to the language with the most major update in 2022 where Generics was introduced into the language (major feature of Java).
Due to increasing and fast development of the language, major companies started using the language into their codebase like Netflix, Microsoft, Cloudfare, Docker, Twitch and Dropbox.
Philosophy of Go
The philosophy of Go and the issues fixed through the langauge is quite interesting:
- Simplicity: Go focuses on simplicity. Every other languages keep adding features which at the end makes it bloated but Go on the other hand only adds features that are considered useful in the long term.
- Efficiency: Go is written to be efficient and faster and its very well evident from its concurrency feature.
- Error Handling: Go also focuses on handling errors like a true boss. It has built-in error handling parameters in most of its built-in functions as a second argument.
- Pragmatism: Go is focused on creating logical and practical solutions to real world problems.
Features of Go:
- Standard Library: Go has a rich amounts of standard libraries ranging from basic math libs to libs for Networking.
- Package Management: It allows managing of user created packages and publishing them with set of commands in the CLI.
- Static-Typed: It assigns types to variables during compile times and looking for type related errors thus fixing issues that come with dynamically typed languages.
- Unit Test Support: Go supports unit tests that run in parallel with the codebase which ensures effective debugging and ensure code quality.
- Platform Independent: Go is platform-independent and can run and compile on any given hardware and Operating System
- Concurrency: They are also called as Goroutines and behave like that of threads that allows running multiple tasks at the same time parallely which is one of the most important features of Golang.
Some of the useful Go commands:
Go run
It is self-evident from the command, this commands compiles and executes the code simulataneously.
go run filename.go
Go build
This command compiles the code and is stored in a executable file.
go build filename.go
Go get
It integrates with GitHub to import libraries defined by other users in GitHub.
go get github.com/libname
Go fmt
This command is really a useful and a interesting feature of Go. This command formats a file with indents and newline characters to make it readable.
go fmt filename.go
Unformatted File
package main
import "fmt"
func main(){
fmt.Println("Sample Addition")
a:=5
b:=4
c:=a+b
fmt.Println(c)
}
Formatted File with go fmt
package main
import "fmt"
func main() {
fmt.Println("Sample Addition")
a := 5
b := 4
c := a + b
fmt.Println(c)
}
You can see how the commands formats the code to make more readable.
What is Go used for?
Go is used for many useful purposes:
- Container Services: Container Services such as Docker and Kubernetes use concurrency for faster containerization.
- Network and cloud services: Go's concurrency features and its APIs make it well suited for writing high perfomance web servers. Bitcoine Lightning Network and other complex blockchain technologies use Go to build them.
- Web services: The HTP server built in the Go standard library makes it wasy for companies like Netflix to build web services and server side programming.
- Command-line tools: Go has many open source libraries for CLI tool development and standard libraries to develop CLI tools.
- Microservices: Go is also used in writing micrservices like FTP and SMTp servers and is used by companies like Uber.
- Data Science: Go's concurrency and memory management makes it a good option for data scientists to analyse large datasets parallely.
How to learn Go
Go is comparetively a easy language to learn. People might find it hard to learn the concepts of Goroutines, Go Channeling and Concurrency as they are the hte hardest concepts of the language and also the best feature of the language.
Hey!! Don't worry, the best features always takes effort to master.
The best ways to learn any language is through books and online documentation. Many don't realise and don't come out of the "Tutorial Hell". People often end up watching too many videos, get confused between concepts and end up procrastinating and finally quit learning the language.
As a person who learns from Books and Online Docs, I could by far say that Golang has one of the best online documentation ever when compared to online docs of any language. They provide concept wise lessons and a IDE interface for learners to execute and see the output of their own.
The docs are divided into two parts: A Tour of Go and Effective Go.
A Tour of Go is from the basics and teaches all the syntaxes one by one which goes from explaining values and datatypes to teaching about Concurrency.
Effective Go teaches us methods to write clear and idiomatic Go Code.
Here are the links to Docs:
- A Tour of Go: https://go.dev/tour/welcome/1
- Effective Go: https://go.dev/doc/effective_go
Books to learn Golang:
This by far the best book to learn Go
- Learning Go: https://www.miek.nl/go/