Structure of a Simple Go program

Let's learn what is the minimum syntax required to get a valid and working Go program.

What You Need

  • A desire to learn Go.

Recipe Guide

A Simple Go Program

Here's a minimal Go program:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Go Go Power Gophers!")
}

Let's walk through the process of building up this program step by step to understand the critical components that make this code a valid Go application.

Breaking Down the Foundation of a Go Application

To build this application we don't need to download any programs or editors. Let's head to the Go Playground and enter our code in there. In the Go Playground, use the Format button to make the code look nice and the Run button to execute it.

Go Package Declarations

The first statement of any Go program is a package declaration:

package name

Packages are a convenient way to organize code logically in Go. A package contains code with related functionality. For example, a package may help perform operations on strings or process input and output from the command line. In the package declaration, the package keyword is followed by the package name. There are tons of standard and custom Go packages out there. However, for this simple program, we use a very special one: main.

The Go main Package

A Go program is created by linking a single, unimported package called main with all the packages it imports. The main package must implement a main function that takes no arguments and returns no value. The main function within the main package is the entry point to the program.

Thus, the most basic Go program would look something like this:

package main

func main() {
}

The program above doesn't do anything but it's a valid Go program. As shown in the initial code snippet, we want to print a string to the console. To do so, we need the Println method that is part of the fmt package.

Importing Packages

fmt is a standard library package that implements methods that handle formatted input and output in Go. To use its methods, we first need to import it:

package main

import "fmt"

func main() {

}

Theimport declaration takes a string that uniquely identifies a package. Now, we can use any of the fmt methods within the body of the main function.

Executing Logic in Go's main Function

Let's update our program to print a string like so:

package main

import "fmt"

func main() {
  fmt.Println("Go Go Power Gophers!")
}

Running our program creates the following output:

Go Go Power Gophers!

That's it. We have created a simple, valid, and functional Go program! To extend this tiny program, we can create functions that are called within the main function to execute sophisticated logic or we can create other packages whose methods are imported and invoked in main as we did with fmt.

Importing individual highly specialized packages is beneficial for the size of our application. Instead of being forced to import everything the Go standard library has to offer, we only import the things that we need. This results in a much smaller and better-performing program.

Recap

As we can see bootstrapping a Go program doesn't require much syntax. The Go programming language was designed to be brief, simple, and fast.

The recipe for a Go program would consist of the following pattern:

// package declaration

// package imports

// Program logic

This design pattern makes the code easier to read, organized, and predictable for you and any other developer needing to work with the code.

I hope you enjoyed reading this bite-sized piece of Go knowledge. See you in the next blog post!