A Comprehensive Introduction to Golang

A Comprehensive Introduction to Golang

An in-depth look at the key features of Go, its installation, and an introductory guide to writing programs.

The genesis of the Go programming language can be traced back to a pivotal moment at Google, where engineers Robert Griesemer, Rob Pike, and Ken Thompson grappled with the intricate nature of C++. Their dissatisfaction with the complexity and the absence of a straightforward, efficiently compiled, and executed language spurred them to embark on a quest to design a novel language. On September 21, 2007, their endeavors culminated in the birth of version 1.0 of the Go language.

The primary mission of the Go creators was to amalgamate the ease of programming found in interpreted, dynamically-typed languages with the efficiency and safety characteristics of statically-typed, compiled languages. Beyond this, the Go language was envisioned to facilitate network and multicore computing, striving to expedite the coding process. To achieve these objectives, Go drew inspiration from various languages, amalgamating their most favorable features into a cohesive, simplified language that resonates with a broad spectrum of developers.

This article takes an in-depth look at the key features of Go, its installation, and an introductory guide to writing programs.

Go

Go is a compiled, concurrent, garbage-collected, statically-typed language. Now, let's break down each of these features to better understand their significance in Go.

  1. Compiled

    In the context of programming, a compiled language relies on a compiler system to translate the written code into machine-readable instructions. These compilers facilitate the transformation of human-readable code into the binary language that a computer understands. This process grants compiled languages an inherent speed advantage during runtime, as they operate directly in machine code. However, a drawback arises: whenever alterations are made to the code, the entire program must undergo a recompilation process, demanding additional time and effort.

    One of the significant advantages of compiled languages lies in the early detection of errors. Before the program is executed, the compiler thoroughly scrutinizes the code, identifying and flagging any potential issues through compilation errors. This results in swift bug detection and resolution, saving time in the development cycle.

    Yet, a notable limitation stems from system dependence. Compiled code is tailored for a specific machine, making it bound to the underlying system's architecture. Consequently, the compiled program might not function properly on systems for which it was not specifically compiled.

    Considering GoLang's nature as a compiled language, it inherently inherits these advantages and trade-offs. The speed, early bug detection, and overall efficiency are key strengths of Go due to its compiled structure. However, developers working with Go need to consider the necessity for recompilation after code changes and be mindful of system dependencies while deploying their programs across different environments.

  2. Concurrent

    Concurrency involves the execution of multiple tasks or instruction sequences simultaneously, distinct from parallelism, although the distinction can be quite nuanced. Concurrency is about dealing with lots of things at once but parallelism is about doing lots of things at once.

    Go, known as a concurrent language, is equipped with tools like Goroutines, simplifying the execution of concurrent programs. Goroutines are basic functions running independently in their threads, allowing for easy handling of concurrency.

    However, managing shared memory, facilitating communication across different threads, and avoiding race conditions present challenges in concurrent programming. To address these issues, Golang offers elegant native tools such as channels, wait groups, and mutex. These features empower programmers to resolve common problems associated with concurrent programs efficiently. Delving deeper into these tools will be covered in subsequent sections of this series.

  3. Garbage-collected

    Garbage collection in programming serves as an automatic memory management process wherein a garbage collector scans and reclaims memory spaces that were initially allocated but are no longer referenced. This mechanism absolves programmers from the intricacies of low-level memory management and cleanups. However, while this feature does save time and effort for developers, it also utilizes system resources to decide which memory to reclaim and perform the cleanup, which could potentially impact a program's performance.

    The primary benefit of garbage collection is its capacity to prevent memory leaks within a program. A memory leak occurs when memory space is lost due to the absence of proper cleanup by the developer, and the program remains unaware of this loss. By implementing garbage collection, Go ensures that memory management becomes a lesser concern for developers, although maintaining vigilance regarding code quality remains essential.

    Nevertheless, there are instances in more advanced scenarios where adjusting or fine-tuning the garbage collection process becomes necessary. This could involve optimizing the garbage collection settings to extract maximum efficiency from a program or even temporarily disabling it to apply alternative memory management techniques. These cases necessitate a more nuanced approach to leverage the full potential of the program.

  4. Statically-typed language

    A statically typed language conducts type checking during the compile time, requiring that the type associated with each variable be determined before the code is compiled. This design necessitates a deliberate and precise approach when defining variables. However, the advantage is that once the variable type is established, it need not be continuously checked during runtime.

    Furthermore, in a statically typed language, once a variable is defined with a certain type, it cannot be altered or assigned data that is inconsistent with that type. This characteristic offers a degree of safety and consistency within the code.

    The perception of static typing varies among developers, as it can be regarded as both a positive and a negative aspect in programming. Some appreciate the rigor and safety it offers by catching potential errors during compilation, while others find it restrictive or overly formal for certain types of coding tasks.

These features collectively position Go as a modern and efficient language, offering speed, concurrency support, automatic memory management, and type safety. Developers can leverage these aspects for different project needs while being aware of associated trade-offs.

Why Golang?

  1. Simplicity

    Golang's appeal lies in its simplicity and straightforward syntax, evident through its concise structure built upon a mere 25 defined keywords. The language fosters a paradigm of singular ways to execute tasks, significantly enhancing code readability while curbing the complexity that often results from over-engineering.

    This dedication to an encouraged singular, effective approach not only streamlines code but also amplifies its reliability. Developers find assurance in the consistent functionality of their code, allowing for a clearer and more predictable development process. What's more, Golang's design philosophy emphasizes simplicity, equipping developers with fundamental tools readily available in its standard library. This emphasis on simplicity reduces the reliance on external or third-party libraries, empowering developers to craft robust applications without unnecessary dependencies.

  2. Golang is fast

    Golang, as a compiled language, offers superior performance compared to dynamically typed languages such as Python and JavaScript. While it may not outperform the fastest languages like C++, the amalgamation of Golang's ease of use and inherent speed renders it significantly rapid and sufficiently swift for a wide array of use cases. This balance between speed and development ease positions Golang as an attractive language for most scenarios, catering to a broad spectrum of practical applications.

  3. Easy Concurrency

    This is a pivotal strength of Golang. Concurrency is deeply ingrained in the language's core, making it an inherent part of its structure. While other languages may achieve concurrent tasks, Golang streamlines this process, requiring significantly less effort and often surpassing the effectiveness of other languages. Golang's innate design offers effortless and native solutions for handling concurrent operations, providing developers with convenient and efficient tools to manage and execute parallel tasks.

  4. Less Memory Footprint

    The benchmark available at https://programming-language-benchmarks.vercel.app/ demonstrates that Golang stands out as one of the most memory-efficient languages presently available. This achievement is attributable to several inherent features of Golang, such as its static typing, robust garbage collection system, and the inherent structuring of data within the language. These traits collectively contribute to Golang's exceptional efficiency in terms of minimal memory consumption compared to other languages.

Companies using Golang

Google, Uber, Paypal, Alibaba, Cloudflare, Docker, Youtube, Linkedin, X (formerlly Twitter), IBM, FireFlies etc

Setting up Go development environment

  1. On Mac

    To install Golang on a Mac, visit https://go.dev/doc/install to obtain the Go installation package. After downloading the package file, proceed to open it, which will effectively install Go on your system. To verify the successful installation of Go, open a terminal and execute the following command:

     $ go version
    

    Upon successful installation, you should receive a response similar to the following, which may vary depending on the installed version of Go:

     go version go1.21.3 darwin/arm64
    

    If you get a command not found error even after installing Go, then you need to add Go to your $PATH.

    run the following commands to do that:

     $ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.zshrc
     $ echo "export PATH=$PATH:$GOPATH/bin" >> ~/.zshrc
     $ source ~/.zshrc
     $ go version
    

    Following these steps, you should see the version of Golang that is currently installed in the console. If you continue to encounter issues, I recommend visiting StackOverflow at https://stackoverflow.com/questions/34708207/command-not-found-go-on-mac-after-installing-go for potential solutions. It's normal to seek help in such situations, and online communities like StackOverflow often provide valuable assistance for resolving technical issues.

  2. on Linux

    To begin using Golang on Linux, head to https://go.dev/doc/install to download the go archive. Then, execute the following command:

     $ rm -rf /usr/local/go
     $ tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz
    

    The initial command removes any existing Golang installation before proceeding with a new installation. The subsequent command untars the recently downloaded archive.

    Please note, execute these commands within the directory where the archive was downloaded. To verify your current working directory, enter the command:

     $ pwd
    

    After the tar operation has completed, you need to add the Go directory to your PATH.

     $ echo "export PATH=$PATH:/usr/local/go/bin" >> $HOME/.profile
    

    Then you can run

     $ go version
    

    to verify that the installation was successful. If you don’t see the current version of Go printed in the console, then play around on Google to fix the issue.

  3. on Windows

    Visit https://go.dev/doc/install to download the GO MSI file, open the file and follow the prompts.

    Then open the Command Prompt and run

     $ go version
    

    You should be greeted with the version of the Go installed.

It is this simple to install Golang.

Coding Tools: Recommended IDE

GoLand by JetBrains is an exceptional IDE designed specifically to facilitate an enjoyable and straightforward Golang coding experience. While this IDE isn't free, its subscription is worthwhile if within your budget. For students, there's an advantageous offer providing a free student license for all JetBrains tools, including GoLand, for a year or more.

If GoLand isn't accessible, Visual Studio Code (VSCode) is an excellent alternative. To obtain VSCode, go to https://code.visualstudio.com/download for setting it up on your machine. Additionally, refer to https://code.visualstudio.com/docs/languages/go for configuring VSCode for Golang development.

Writing and running your first Go program

Our first Golang program is going to be a simple Hello World, to show you the feel of Golang. In addition, we will code a GPA Calculator to give you a more robust introduction to coding in Go.

Let’s start with the HelloWorld program:

  1. Create a folder for our program.

    open your terminal, and run the following command:

     $ mkdir golang_projects && cd golang_projects
     $ mkdir hello_world && cd hello_world
    

    The mkdir is a command to create a directory with the name of the directory following it, because as software engineers, we should be comfortable with using the terminal to get things done (and it is usually faster than GUI). The && , the logical AND, is a syntax to concatenate two commands in the terminal, meaning we are telling the terminal to create a directory AND cd into the directory. As you may have figured, cd is a command to go into the directory.

  2. then run

     $ go mod init github.com/hello_world
    
     ## response
     go: creating new go.mod: module github.com/hello_world
    

    This command tells Go to treat the folder we just created as a module, and to do the initialization of the dependency management for our project, so that when we import packages in other modules, go can know how to manage them for us. We will look into Go’s dependency management in detail later in this series.

  3. create a file hello.go in the directory

     $ touch hello.go
    

    touch is a command to create a file from the terminal. Open the directory in your favourite IDE and open the hello.go file we just created.

  4. Write the following code in the file

     package main
    
     import "fmt"
    
     func main() {
         fmt.Println("Hello, World!")
     }
    

    The line package main serves as a directive to Go, signifying that this file belongs to the main module. A package acts as a means to collect functions and essentially represents files located within the same directory in Go.

    The statement import "fmt" functions by importing the "fmt" package into our code. This is how we use other codes written by other people in our program. import enables us to incorporate functionalities written by others into our program. "fmt" is part of the standard library inherent to Go, encompassing various formatting functions, notably the Println function, which outputs content to the console.

    For those transitioning from other programming languages, the use of a capital letter to initiate a method might be surprising. In Go, the casing of the initial letter of a type determines the scope at the file level. Types beginning with a capital letter are accessible and exposed beyond their original declaration, while types starting with a lowercase letter remain unexposed and cannot be accessed outside of their specific scope. This mechanism mirrors the idea of public and private (or _) in languages like Java and others.

    The func keyword is used to formally declare a function. Each time you intend to create a function, you must use this keyword for declaration. In our code, we declare the main function, which commonly serves as the entry point in a Golang program. By default, it gets executed whenever you run the main package.

    fmt.Println("Hello, World!") too is self-explanatory, we are calling the Println method of the fmt package to print the string “Hello, World!” to the console.

  5. To run the program, open a terminal in the main module (the directory where the go.mod exists), run the command below and you should see a beautiful “Hello, World!” printed in the console.

     $ go run .
    
     ## resp
     Hello, World!
    

    The command run compiles and runs the go module declared in the current working directory, specified by the dot (.)

    And that is your first Golang program. Despite the simplicity of this program, it is a good entry point to the world of Golang.

Check the next episode for the simple but explanatory GPA calculator that will make you fall in love with Go. Keep GOing.

References:

The Go programming language—everything you should know (https://codilime.com/blog/what-is-go-language/)

Go (programming language) https://en.wikipedia.org/wiki/Go_(programming_language)

Concurrency (computer science) https://en.wikipedia.org/wiki/Concurrency_(computer_science)

Concurrency in Operating System https://eng.libretexts.org/Courses/Delta_College/Operating_System%3A_The_Basics/05%3A_Process_Synchronization/5.1%3A_Introduction_to_Concurrency

Dynamic typing vs. static typing https://courses.cs.washington.edu/courses/cse341/04wi/lectures/13-dynamic-vs-static-types.html

Tutorial: Get started with Go https://go.dev/doc/tutorial/getting-started