Go Series: An approach to the Golang Programming Language

🐹 The Go core, WebDev, GameDev, CliDev, GUIDev and different examples.


Go Series: An approach to the Golang Programming Language


Today we are going to start a series about several different programming languages. These are documents that I have been writing for a long time and that I didn’t know what to do with.

Some people suggested doing a Course, but that would take me even more time, I might even turn it into a video in the future, if possible. But, to avoid having all this content sitting here, I’m going to start “releasing” it as posts.

The difference in these series is that the examples are different from what you can find out there. They are things that I created myself and people like the things I do, I think it must be the experience I have had since 2011 when I started Terminal Root, in fact, it started like that, things that I did for myself and decided to save on Blogger and without meaning to, the links were already in several forums, which is when I discovered why my blog was getting a lot of traffic.

In this first series we will cover the programming language Go/Golang.

The Go programming language is responsible for many things that many of us use on a daily basis. Several famous software programs are made with Go, among them, we have:

  • Docker - Operating system-level virtualization.
  • Kubernetes - Widely used container orchestrator.
  • Terraform - Infrastructure as Code (IaC) tool from HashiCorp.
  • Prometheus - Monitoring and alerting system.
  • Grafana Loki – Scalable logging system. + Hugo – Fast static site generator.
  • [Gogs/Gitea – Self-hosted Git repository management platform.

The latest is TypeScript Go, a TypeScript runtime made by Microsoft, which will be the default for running in applications such as: VS Code and others.

This TypeScript port for Go has been the talk of the town in recent days, as TypeScript execution has become 10x faster!

In addition to several other lesser-known ones.

One of the differences in the Go programming language is that, unlike languages ​​such as: Swift, Rust, Zig, and others, Go DOES NOT DEPEND ON LLVM/C++ to compile and create its binary files; the entire structure of the language is original and proprietary.

In addition to not having resorted to the C++ invention of deleting pointers and instead using Garbage Collector, which has a small impact on performance, but does not run the severe risk of memory leaks. For those who still do not understand how this works, know that Rust, which is seen as memory safety, has a page in its documentation warning that it is possible to have memory leaks.

The only difference is that the original compiler, like Swift, Carbon, Rust, Zig and others, was written in C++ which was gofrontend, but it has also “suffered”: bootstraping.

In these episodes of the series we will try to cover 3 to 4 topics. In today’s Episode 01 we will see:


01. Introduction

Go is a programming language created by Google and released as open source in November 2009.

It is a compiled language focused on productivity and concurrent programming, based on work done in the operating system called Inferno.

The initial design of the language was done in September 2007 by Robert Griesemer, Rob Pike and Ken Thompson (one of the creators of UNIX).

Currently, there are implementations for Windows, macOS, GNU/Linux and FreeBSD.

For more information see here, the official Go page is: https://go.dev/


02. Preparing the Environment

First of all, you need to have Go installed on your system. You can download it here: https://go.dev/dl/ or use the package manager of your operating system, examples:

winget install --id=GoLang.Go -e
brew install go
sudo apt install golang-go

After installing, test by running the go version command. If the version similar to the content below appears, then everything is ok.

go version
go version go1.22.2 amd64

Alternatively, you can also use the GCC compiler, which is GCCGO, for this see the links below:

It is also interesting to install snippets for Go in your favorite IDE/Editor.


03. Hello, World!

The most basic program of all is always a Hello, World!, so let’s create our example with Go. Create a file with any name and extension .go, for example: hello.go and insert the content below:

package main

import "fmt"

func main (){
fmt.Println("Hello, Go!")
}

To run it you can run the command below, although it is not the best way, as it is slow, but it is possible:

go run hello.go

The correct way is to build the binary (it is faster) and run it later:

go build hello.go
./hello

Explaining the code:

  • package main → Each Go program is composed of packages. Programs start running from the main package.
  • import "fmt" → This program is using the package with import path “fmt”, alternatively you could use the syntax: import ("fmt") used for more than one import.
  • func main() → This is the main function, it always needs to exist in all Go programs, we need to open it with { and close it with }. + fmt.Println("Hello, Golang!") → To print the desired content, use fmt.Println(), the Println() function is part of the fmt library, the capital P must be respected.


04. Comments

Go uses C/C++ style to write comments (part of the code that will not influence the result, only for informational purposes for whoever comes to read the code) // comment or /* comment */ (multiple lines), examples:

// My first Go code
package main // import the main package

import "fmt" // using the fmt library for Println

func main (){ // main function
fmt.Println("Hello, Golang!") // Printing
}
/*
To run and/or compile, use respectively
$ go run hello.go

OR

$ go build hello.go
$ ./hello

*/

Okay, for this Episode 01 of the Go Series it’s already good enough, I’ll wait for you in the next episodes!


go series-go


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles