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.

Getting Started With Go

The Go programming language isn’t pre-installed on Windows, macOS, or most Linux distros. You’ll need to install Go to run Go programs. You can check the official Go downloads page to install it on your machine. You’ll want to install a recent version of Go to get the most from the language since newer versions have more features and updates.

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

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 a go.mod file at the root. That file defines the module’s path, the import path, and the dependency requirements for a successful build process.

You can create a Go modules file with the Go mod command and the init subcommand before specifying the path or directory for the project.

go mod init project-directory

The command creates the go.mod file. The argument after the init command 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 the require declaration in the go.mod file to ensure they’re included.

You can use the tidy subcommand of the mod command to download all dependencies required for your program.

go mod tidy

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

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.

package folder

func Folder() any {
return 0;

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

package directory

func directory() {
folderFunc := folder.Folder()

You’ll have to export the identifier by capitalizing the name to access an identifier in an external namespace.

The Main Function

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

Here’s a simple Hello World Program to demonstrate the main function:

package main
import "fmt"

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

This code declares the main function in the main package namespace. It then imports the fmt package and uses the Println method to output a string to the console.

Importing Packages in Go

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

import "fmt"

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

import (

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.

import (
encoder "encoding/json"

Here, you imported the json package with the custom name as encoder. 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.

import (
_ "fmt"

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

Go Run vs. Go Build

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

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

go run file.go 
go run packagename

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

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

go build main.go 
go build "package name"

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

You can specify a directory as an argument, and the build command will output the executable in the specified directory.

go build file -o "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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button