Understanding a Minimal Go Program

Go’s popularity has increased over the years. More companies are adopting Go, and the developer pool is growing as more use cases for the language roll out. People use it to build programs ranging from fast web apps, blockchain solutions, and machine learning tools.

Developers love Go because of its expressivity, ease of use, and high language performance. Go has a faster learning curve than most languages with a fast-growing ecosystem of packages and libraries.

4

Getting Started With Go

The Go programming languageisn’t pre-installed on Windows, macOS, or most Linux distros. You’ll need to install Go to run Go programs. You can check the officialGo downloadspage to install it on your machine. You’ll want to install a recent version of Go to get the most from the language sincenewer versions have more featuresand updates.

Once you have Go installed, you can work with the language, execute Go files, create workspaces, and build projects in Go.

MacBook and a Dell laptop running ZorinOS next to each other

You can create a Go file and run Go programs using the standard library. However, if you want to use external libraries, you’ll need to create a Go modules file for dependency management, version tracking, and more.

The Go Modules File

In Go, a module is a collection of packages in a file tree with ago.modfile at the root. That file defines the module’s path, the import path, and the dependency requirements for a successful build process.

it’s possible to create a Go modules file with the Gomodcommand and theinitsubcommand before specifying the path or directory for the project.

Dell monitor showing Windows 10 desktop

The command creates thego.modfile. The argument after theinitcommand is the module path. The module path can be the file path on your host environment, or the repository domain path.

When you install external packages and dependencies, go will update therequiredeclaration in thego.modfile to ensure they’re included.

firefox logo with yellow warning symbol

you could use thetidysubcommand of themodcommand to download all dependencies required for your program.

The command will download all missing imports to the go modules file.

The Go programming language mascot, a gopher with prominent front teeth and large eyes

The Go Package Namespace

Every Go source file belongs to a package, and you can access code within a package namespace using its identifier.

You can have multiple namespaces for your packages. Once you create a folder, you’ve created a new namespace. You can access other namespaces with a dot (.) notation.

Here’s an example of accessing a different namespace from another namespace.

You’ll have toexport the identifierby capitalizing the name to access an identifier in an external namespace.

The Main Function

Themainfunction serves as the entry point for Go programs. You can’t execute a Go file or package without the main function. You can have amainfunction in any namespace; however, you must have only onemainfunction in a file or package.

Here’s a simpleHello World Programto demonstrate themainfunction:

This code declares themainfunction in themainpackage namespace. It then imports thefmtpackage and uses thePrintlnmethod to output a string to the console.

Importing Packages in Go

Compared to many other languages, importing packages and dependencies is easy. Theimportkeyword provides functionality for importing packages. You can import packages from the standard library and external dependencies with theimportkeyword.

In the above example, you’re importing one package. You can import multiple packages. You’ll have to specify the packages in parentheses after theimportstatement.

Adding any delimiters in import statements is invalid. You can declare a custom name for imports by specifying the custom name before the package name.

Here, you imported thejsonpackage with the custom name asencoder. You’ll have to access the package’s functions and types with the custom name (encoder).

Some packages require you to import other packages for side effects. You’ll have to prepend the package name with an underscore.

you may’t access packages you’ve imported for side effects, but dependencies can if you configure them.

Go Run vs. Go Build

You’ll use therunandbuildcommands to compile and execute your Go code. They have similar functionalities, and you’ll use them for executing packages.

Theruncommand is a combination of compilation and execution instructions. Theruncommand executes the package without creating any executables in the working directory. You’ll have to specify the file name of the package name after theruncommand.

Thebuildcommand is a compilation command that compiles a package or file into a binary executable.

If you run thebuildcommand without any arguments after the file or package name, go will generate an executable in your package’s root directory.

You’ll have to recompile the program with thebuildcommand when you change a package.

you could specify a directory as an argument, and thebuildcommand will output the executable in the specified directory.

There’s So Much You Can Do With Go

The Go standard library is powerful and intuitive. You can quickly build modern applications without having to install any external dependencies.

Since Go’s release in 2009, developers and companies have used it for various use cases, in a range of fields. Its success is primarily because Go provides a Python-like syntax alongside C-like performance.

Should you learn Go? Find out more about job prospects, level of difficulty, and Golang-specific features in this article.

Tor spoiled me forever.

Quality apps that don’t cost anything.

Flagship price, mid-range phone.

Goodbye sending links via other apps.

My iPhone does it all, but I still need my dumb phone.

Technology Explained

PC & Mobile