1. Learn the Basics

Learn the common concepts of Go like variables, loops, conditional statements, functions, data types, and so on. A good starting point for go basics is its Go’s official docs.

◇Basic Syntax

Learn about the basic syntax of Go, such as how the Go programs are executed, package imports, main function, and so on.

◇Variables in Go

Variable is the name given to a memory location to store a value of a specific type. Go provides multiple ways to declare and use variables.

◇Data Types(bool | int, int8/16/32/64 | byte | unit, unit8/16/32/64 | rune | float32/64 | complex64/128 | unitptr)

Go is a statically typed programming language, which means each variable has a type defined at first and can only hold values with that type. There are two categories of types in Go: basics types and composite types.

◇For Loop

Go has only one looping construct, the for loop. The basic for loop has three components separated by semicolons:

◇Range

range is used with for loops to iterate over each element in arrays, strings and other data structures.

◇Conditional Statements

Conditional statements are used to run code only if a certain condition is true. Go supports:

◇Errors/Panic/Recover

In lieu of adding exception handlers, the Go creators exploited Go’s ability to return multiple values. The most commonly used Go technique for issuing errors is to return the error as the last value in a return.

A panic typically means something went unexpectedly wrong. Mostly used to fail fast on errors that shouldn’t occur during normal operation, or that we aren’t prepared to handle gracefully.

Panic recovery in Go depends on a feature of the language called deferred functions. Go has the ability to guarantee the execution of a function at the moment its parent function returns. This happens regardless of whether the reason for the parent function’s return is a return statement, the end of the function block, or a panic.

◇Functions, multiple/named returns

A function is a block of code that performs a specific task. It’s a reusable unit of code that can be called from other parts of your program. Functions help you organize your code, make it more modular, and improve readability.

◇Packages, imports and exports

Packages are the most powerful part of the Go language. The purpose of a package is to design and maintain a large number of programs by grouping related features together into single units so that they can be easy to maintain and understand and independent of the other package programs. This modularity allows them to share and reuse. In Go language, every package is defined with a different name and that name is close to their functionality like “strings” package and it contains methods and functions that only related to strings.

◇Type Casting

Go doesn’t support automatic type conversion, but it allows type casting, which is the process of explicitly changing the variable type.

◇Type Inference

Type inference gives go the capability to detect the type of a value without being explicitly indicated , hence the possibility to declare variables without providing its type at first

◇Arrays

In Go an array is a collection of elements of the same type with a fixed size defined when the array is created.

◇Slices

Slices are similar to arrays but are more powerful and flexible. Like arrays, slices are also used to store multiple values of the same type in a single variable. However, unlike arrays, the length of a slice can grow and shrink as you see fit.

◇Maps

Maps are the data structure in Go, where we use whenever we want to have mappings between key:value pairs. They have flexibility in terms of removing or adding elements into them. Maps do not allow duplicate entries while data are kept unordered.

◇make()

Golang’s built-in function make, helps us create and initialize slices, maps and channels, depending on the arguments that are provided to the function.

◇Structs

Structs are user-defined types that help us create a collection of data describing a single entity.

2. Going Deeper

Diving deeper into Golang involves exploring the language’s more advanced features and best practices to harness its full potential. Once you’ve mastered the basics, advancing your knowledge includes understanding complex aspects of Go that can significantly improve your code’s performance, readability, and maintainability.

◇Go Modules

Go modules are a group of related packages that are versioned and distributed together. They specify the requirements of our project, list all the required dependencies, and help us keep track of the specific versions of installed dependencies.

◇Marshalling & Unmarshalling JSON

JSON (JavaScript Object Notation) is a simple data interchange format. Syntactically it resembles the objects and lists of JavaScript. It is most commonly used for communication between web back-ends and JavaScript programs running in the browser, but it is used in many other places, too.

◇Types, Type Assertions, Switches

Types in Golang specify the data type that a valid Go variable can hold. Golang has four categories of Types including Basic, Aggregate, Reference, and Interface Types. Type assertions in Golang provide access to the exact type of variable of an interface.

◇Interfaces

An interface in Go, is a type that defines a set of methods. If we have a type (e.g. struct) that implements that set of methods, then we have a type that implements this interface.

◇Context

The context package provides a standard way to solve the problem of managing the state during a request. The package satisfies the need for request-scoped data and provides a standardized way to handle: Deadlines, Cancellation Signals, etc.

◇Goroutines

Goroutines allow us to write concurrent programs in Go. Things like web servers handling thousands of requests or a website rendering new pages while also concurrently making network requests are a few example of concurrency. In Go, each of these concurrent tasks are called Goroutines.

◇Channels

Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.

Channels are a typed conduit through which you can send and receive values with the channel operator, <- .

◇Buffer

The buffer belongs to the byte package of the Go language, and we can use these package to manipulate the byte of the string.

◇Select

The select statement lets a goroutine wait on multiple communication operations.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready. The select statement is just like switch statement, but in the select statement, case statement refers to communication, i.e. sent or receive operation on the channel.

◇Mutex

Go allows us to run code concurrently using goroutines. However, when concurrent processes access the same piece of data, it can lead to race conditions. Mutexes are data structures provided by the sync package. They can help us place a lock on different sections of data so that only one goroutine can access it at a time.

◇Scheduler

Go Scheduler allows us to understand more deeply about how Golang works internally. In terms of logical processors, cores, threads, pool cache, context switching etc. The Go scheduler is part of the Go runtime, and the Go runtime is built into your application.

◇Generics

Go Generics is a feature that allows you to write functions, data structures, and algorithms that can work with any type. This is a powerful feature that can help you write more flexible and reusable code.

◇Pointers

Go pointers are a powerful feature that allows you to work with memory addresses directly. They are used to store the memory address of a variable. This can be useful when you need to pass a large amount of data to a function or when you need to modify the value of a variable inside a function.

3. Building CLI Applications

Command line interfaces (CLIs), unlike graphical user interfaces (GUIs), are text-only. Cloud and infrastructure applications are primarily CLI-based due to their easy automation and remote capabilities.

Go applications are built into a single self contained binary making installing Go applications trivial; specifically, programs written in Go run on any system without requiring any existing libraries, runtimes, or dependencies. And programs written in Go have an immediate startup time—similar to C or C++ but unobtainable with other programming languages.

◇Cobra

Cobra is a library for creating powerful modern CLI applications.

◇Urfave CLI

Urfave CLI is a simple, fast, and fun package for building command line apps in Go.

4. ORMs

Object–relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between type systems using object-oriented programming languages. This creates, in effect, a “virtual object database”, hence a layer of abstraction, that can be used from within the programming language. Most common ORM library in Go is GORM.

◇GORM

The GORM is fantastic ORM library for Golang, aims to be developer friendly. It is an ORM library for dealing with relational databases. This gorm library is developed on the top of database/sql package. The overview and feature of ORM are: Full-Featured ORM (almost)

5. Web Frameworks

There are several famous web frameworks for Go. Most common ones being:

◇Beego

Beego is used for rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services. It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific features such as interfaces and struct embedding.

◇Gin

Gin is a high-performance HTTP web framework written in Golang (Go). Gin has a martini-like API and claims to be up to 40 times faster. Gin allows you to build web applications and microservices in Go.

◇Revel

Revel organizes endpoints into Controllers. They provide easy data binding and form validation. Revel makes Go Templates simple to use at scale. Register functionality to be called before or after actions.

◇Echo

Echo is a performance-focused, extensible, open-source Go web application framework. It is a minimalist web framework that stands between stdlib + router and a full-stack web framework.

◇Gofiber

Go Fiber is an Express-inspired framework for Golang. Go Fiber is a web framework built on top of fast HTTP. It can be used to handle operations such as routing/endpoints, middleware, server request, etc.

◇Gorilla

Gorilla is a web toolkit for the Go programming language that provides useful, composable packages for writing HTTP-based applications.

6. Logging

Go has built-in features to make it easier for programmers to implement logging. Third parties have also built additional tools to make logging easier.

◇log/slog

The log and log/slog (since go 1.21) packages are the standard logging packages in Go. These packages provides a simple logging API that can be used to log messages to the console or to a file.

◇Zap

Blazing fast, structured, leveled logging in Go.

  • OpenSource Zap

◇Zerolog

The zerolog package provides a fast and simple logger dedicated to JSON output.

Zerolog’s API is designed to provide both a great developer experience and stunning performance. Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection.

7. Real time communication

Just as it says in the name, real-time communication is the handling of requests concurrently and efficiently. Whether it is a chat/messaging app, an email service, a game server or any collaborative online project (for example, Excalidraw), there are a few different ways of handling real-time communication, but the most common is through the use of WebSockets. Other options for handling real-time communications include MQTT protocol and server-sent events, among others.

◇Melody

Melody is websocket framework based on gorilla/websocket that abstracts away the tedious parts of handling websockets. It gets out of your way so you can write real-time apps.

◇Centrifugo

Centrifugo is an open-source scalable real-time messaging server. Centrifugo can instantly deliver messages to application online users connected over supported transports (WebSocket, HTTP-streaming, SSE/EventSource, GRPC, SockJS, WebTransport). Centrifugo has the concept of a channel – so it’s a user-facing PUB/SUB server.

8. API Clients

An API client is a set of tools and protocols that operate from an application on a computer. They help you to bypass some operations when developing a web application rather than reinventing the wheel every time. Using a client API is a great way to speed up the development process.

◇REST

REST (Representational State Transfer) API (Application Programming Interface) is used to deliver user functionality when dealing with websites. HTTP requests are used to communicate with REST APIs so users can navigate a URL website. These URLs can return certain information that is stored as part of the API.

Heimdall

Heimdall is an HTTP client that helps your application make a large number of requests, at scale. With Heimdall, you can:

  • Use a hystrix-like circuit breaker to control failing requests

  • Add synchronous in-memory retries to each request, with the option of setting your own retrier strategy

  • Create clients with different timeouts for every request All HTTP methods are exposed as a fluent interface.

  • OpenSource Heimdall

Grequests

Golang implementation of Python Grequests library(one of well known HTTP Library in Python).

Features:

  • Responses can be serialized into JSON and XML

  • Easy file uploads

  • Easy file downloads

  • Support for the following HTTP verbs GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS

  • OpenSource Grequests

◇GraphQL

GraphQL is a query language for APIs, it offers a service that prioritizes giving just the data that the client requested and no more. Besides, you don’t need to be worried about breaking changes, versioning and backwards compatibility like REST APIs. Therefore you can implement your version and auto-document your API just by using GraphQL.

Graphql-go

A GraphQL package for Go.

Gqlgen

According to their documentation, it’s a Golang library for building GraphQL servers without much effort.

9. Testing Go Code

Go has a built-in testing command that we can use to test our program.

10. Tools for Microservices

Microservices are an architectural approach to software development that allows the creation of a distributed application from deployable services that allow communication through a well-defined API. Being a solution to monoliths.

◇Watermill

Watermill is an event streaming library for handling asynchronous requests in Go. It provides multiple implementations for pub/sub. For example, you can use conventional pub/sub systems like Kafka or RabbitMQ, as well as HTTP or MySQL binlog, depending on your use case.

◇Rpcx

Rpcx is a RPC (Remote Procedure Call) framework like Alibaba Dubbo and Weibo Motan. Some of the advantages on using Rpcx:

  • Simple: easy to learn, easy to develop, easy to integrate and easy to deploy

  • Performance: high performance (>= grpc-go)

  • Cross-platform: support raw slice of bytes, JSON, Protobuf and MessagePack. Theoretically it can be used with java, php, python, c/c++, node.js, c# and other platforms

  • Service discovery and service governance: support zookeeper, etcd and consul.

  • official Rpcx Official Website

  • official Rpcx Documentation

  • OpenSource Rpcx

◇Go-kit

Go kit is a programming toolkit for building microservices (or elegant monoliths) in Go. it solves common problems in distributed systems and application architecture so you can focus on delivering business value.

◇Micro

It is an API first development platform. It leverages the microservices architecture pattern and provides a set of services which act as the building blocks of a platform.

◇go-zero

go-zero is a web and rpc framework with lots of engineering best practices builtin. It’s born to ensure the stability of the busy services with resilience design, and has been serving sites with tens of millions users for years.

◇Protocol Buffers

Protocol Buffers(Protobuf) is a free, open-source, language-neutral, platform-neutral, extensible data format used to serialize structured data. It’s like JSON, except it’s smaller and faster, and it generates native language bindings.

Some of the advantages of using protocol buffers include:

◇gRPC-Go

Go language implementation of gRPC(gRPC is a technology for implementing RPC APIs).

◇Grpc gateway

gRPC-Gateway creates a layer over gRPC services that will act as a RESTful service to a client. It is a plugin of protoc. It reads a gRPC service definition and generates a reverse-proxy server which translates a RESTful JSON API into gRPC.

◇Twirp

Twirp is a framework for service-to-service communication emphasizing simplicity and minimalism. It generates routing and serialization from API definition files and lets you focus on your application’s logic instead of thinking about folderol like HTTP methods and paths and JSON.