Go Architecture Explained

vishal rana
6 min readJan 26, 2023

Go or Golang is one of the language for which adoption is increasing day by day at a rapid rate. Go was also developed with a focus on portability and compatibility.

In this blog, we’ll go through how go works under the hood and what is the architecture behind it that makes it fast and reliable.

Let’s jump right into it.

Origin Of Golang

The Go programming language, also known as Golang, was developed by Google and was first released in 2009. It is a statically-typed, compiled language that is designed to be simple, efficient, and easy to use.

Go was developed by Google as a response to the challenges they were facing with their existing infrastructure and the limitations of existing programming languages.

One of the main reasons for developing Go was to improve the performance of large, distributed systems. At the time, Google was dealing with a growing number of servers and services, which required a lot of concurrent and parallel processing. Existing languages, such as C++ and Java, were not well suited for this kind of workload, as they had complex and error-prone concurrency models.

Go was designed with a simple syntax, a small set of keywords, and built-in support for common tasks such as testing and garbage collection which makes it simple to learn.

How does it works?

Go is a compiled language, means it is compiled down to machine code easily and it generate executable binaries that can run on any system without installing go runtime on your system.

For example, if you have compiled a Go program for Linux, it can be run on any Linux machine without the need to install Go or any other dependencies. Similarly, if you have compiled a Go program for Windows, it can be run on any Windows machine without the need to install Go or any other dependencies.

Let’s understand step by step how it works:

  • The Go compiler reads the source code and converts it into an intermediate representation (IR) using a parser.
  • The IR is then passed to the semantic analysis stage, where the compiler checks for errors and resolves any symbols or names used in the code.
  • The next stage is code generation, where the compiler generates machine code from the IR. This process includes optimization, such as inlining and constant folding.
  • The final stage is linking, where the compiler combines the machine code for all the package dependencies and creates a single executable binary.
  • When the program runs, the Go runtime system is initialized, which includes things like the garbage collector, memory allocation, and the scheduler for goroutines.

Go’s concurrency model is based on Communicating Sequential Processes (CSP) which is a mathematical model of computation that helps to simplify concurrent programming by making it explicit how different processes communicate with each other. This can help to improve the scalability and the robustness of concurrent systems.

Go uses goroutines and channels for concurrency. Go is known for its concurrency. Go routines and channels can be used for parallelism without the need for locks or other synchronization mechanisms.

Go uses a garbage collector that runs in the background which means that it doesn’t introduce any additional overhead or latency to the program.

Go also has a small runtime and a small set of built-in libraries, which results in fast startup times and minimal overhead.

NO VIRTUAL MACHINE

Go does not have a virtual machine. It means it is directly converted to machine code, rather than other programming languages like JAVA.Java code is compiled to bytecode, which is then run on the Java Virtual Machine (JVM). The JVM interprets the bytecode and executes the corresponding machine code on the host platform.

Let’s try to understand with an example:

Let’s say you have a Go program that performs a simple mathematical calculation, such as adding two numbers together. When you compile and run the program, the Go compiler will generate machine code that directly performs the calculation. On the other hand, if you were to write the same program in Java, it would be compiled to bytecode, which would then be interpreted by the JVM at runtime to perform the calculation.

What are the use cases of this language?

  1. Web development: Go is well-suited for building web servers and web services. It has a built-in support for HTTP and a standard library that provides a simple and efficient way to build web applications.
  2. Systems programming: Go is well-suited for building low-level systems, such as operating systems, network servers, and device drivers. Its built-in support for concurrency and parallelism makes it a good choice for building high-performance systems.
  3. Networking and distributed systems: Go’s built-in support for concurrency and parallelism, along with its low-level control over sockets and other networking primitives, make it a good choice for building networking and distributed systems.
  4. Data processing and analysis: Go’s efficient memory management and built-in support for concurrency and parallelism make it a good choice for building high-performance data processing and analysis applications.
  5. Micro-services: Go’s lightweight and efficient runtime and its built-in support for concurrency and parallelism make it a good choice for building microservices.
  6. Command-line utilities: Go’s small runtime and efficient memory management make it a good choice for building small command-line utilities that perform specific tasks.
  7. Machine learning and AI: Go’s efficient memory management and built-in support for concurrency and parallelism make it a good choice for building machine learning and AI applications that require high-performance and scalability.
  8. Game development: Go’s built-in support for concurrency and parallelism makes it a good choice for building high-performance games.

Go’s popularity is increasing in the industry, it’s now being adopted by many companies and organizations, including Google, Uber, Netflix, and many others.

What are the tradeoffs?

With great features, there are some tradeoffs as well that comes with go. We have to go through the tradeoffs as well before choosing a programming language in order to get the work done.

Every programming language has tradeoffs, we just need to choose one that works for us and tradeoffs for that language are not a blocker for us.

  • Lack of generics: Go does not have built-in support for generics. But there are workarounds for it.
  • Limited exception handling: Go does not have built-in support for exceptions. Instead, it uses a combination of return values and panic/recover to handle errors.
  • Limited OOP support: Go is not a pure object-oriented language, it supports structs and methods on structs, but it doesn’t have inheritance or interfaces, which means that you have to use composition and interfaces to build your applications.
  • Strictly typed: Go is a strictly typed language, which means that you have to explicitly specify the type of every variable and function argument. If you are coming from a more dynamic typed language like javascript then this is going to be new for you but typescript basically covers it. so you are good here as well.

Conclusion

Go is a good programming languages that give you much better performance and is easy to learn as well. If you are looking for a new programming language to learn this year, Go would be a great choice for you.

If you like this blog and interested in reading more content like this do check out my other blogs & follow for email updates.

Do checkout these blogs:

Other Architecture blogs:

Other Life changing blogs for productivity and Focus:

--

--