In this topic, we will build our very first Go program from scratch. We'll start by setting up a project directory, then initialize a Go module, create some packages, and finally compile our program.
Creating a workspace
We will begin building our Go program by setting up a workspace for our project. Let's create a new project directory named example and then move into it.
$ mkdir example
$ cd example
In simple terms, a workspace is a directory that contains a number of related sub-directories and files. The workspace directory is the root of our Go project.
The next step is to create a Go module for our project. This module will group related packages that we will use in our program, and also let us install external dependencies. Let's execute the following command to create a Go module named example.
$ go mod init example
Now it's time to create some packages that our program can use. Packages in Go are simply directories in our project containing one or more Go source files. Let's create the hello package.
$ mkdir hello
Great! We have finished setting up the workspace for our first Go program. The next step is writing some code for our newly created packages.
Creating packages
Let's go ahead and create the contents of the hello package first. Within the newly created hello directory, let's create a new file called hello.go with the following contents:
package hello
const Welcome = "My first Go program!"
func HelloWorld() string {
return "Hello, World!"
}
Inside the hello.go file we have created a variable Welcome that any Go source file or package within our example project can access. We have also created the HelloWorld() function that returns a string "Hello, World!" that we will be calling from our main package in the next step.
Now let's move back to the root directory, and create a new file called main.go with the following contents:
package main
import (
"fmt"
"example/hello"
)
func main() {
fmt.Println(hello.Welcome)
fmt.Println(hello.HelloWorld())
}
In Go, the main package is a special executable package that has to comply with two requirements: the package name must be main, and it must contain a function called main(), which will be the entry point for our program.
Let's go through the contents of the main.go file in detail :
-
In the first line, we have declared the
package main, which means this will be an executable package. -
In the third line, we have imported the
fmtand theexample/hellopackage that we previously created. This import statement allows us to use the functions from thefmtpackage such asPrintln(), and also allows us to use the functions and access variables declared in theexample/hellopackage. -
In the ninth line, we have declared
func main(): this is the starting point of our Go program. Within our main function, we can use the functions and variables from the imported packages. We start the code of our main function by printing the contents of theWelcomevariable, and after that, we print theHelloWorld()function, both from theexample/hellopackage.
Upon creating these two packages, this is how our project workspace should currently look like:
example/
├── go.mod
├── hello
│ └── hello.go
└── main.go
So far so good! We are almost done creating our program, let's move on to the next step.
Compiling the program
Finally, we can build and compile our first Go program. Let's build our program by executing the following command:
$ go build main.go
This will create a binary file named main in the root directory where our main.go file is located. Let's execute our Go program by running the following command in our terminal:
$ ./main
My first Go program!
Hello, World!
go build command will only create an executable binary file if the Go source file that we're compiling has the package main declaration and the main() function!In case we want to compile and execute our program, but not create a binary executable file in the current directory, we can use the go run command. A simple explanation of how the go run command works is that it will compile the Go source file, create a binary executable file in a temporary directory in our computer, and after that automatically execute it.
$ go run main.go
My first Go program!
Hello, World!
Remember that the go run command has the same requirements as the go build command in order to properly compile and execute a Go source file.
Congratulations! you have created and compiled your first Go program, but we're not done yet! Let's add a few more features to our program by installing external dependencies.
Adding more features
When we start working on bigger Go projects in the future, often we'll need to install into our project external packages created by other developers.
Since we have initialized a Go module in our project, we'll be able to easily handle external dependency management. Let's go ahead and install the github.com/fatih/color external package hosted on GitHub, via the go get command:
$ go get github.com/fatih/color
After executing this command, our go.mod file contents will update and include the fatih/color package requirement. Let's take a look at the contents of our go.mod file:
module example
go 1.17
require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
Do not worry about the indirect comment! This just means we haven't imported and used the fatih/color package within our Go program yet.
The go.mod file within our project will list all the external package dependencies we have installed in our project.
The next step is to import and use the fatih/color package within our main.go file:
package main
import (
"fmt"
"example/hello"
"github.com/fatih/color"
)
func main() {
fmt.Println(hello.Welcome)
fmt.Println(hello.HelloWorld())
color.Blue("I'm blue daba dee daba di!")
}
Finally, let's build and compile our Go program once again via the go build main.go command, and after that let's execute it in our terminal:
$ ./main
My first Go program!
Hello, World!
I'm blue daba dee daba di!
In our program output, we will see a new blue-colored line: I'm blue daba dee daba di! this line is blue-colored because we've printed it using the color.Blue() function from the newly imported fatih/color package.
Way to go! We have created a Go project that contains all the key aspects required in proper software development.
Conclusion
Let's break down what we've learned so far:
-
Creating a workspace for every new project helps us keep a clean and organized codebase;
-
Initializing a module will group all related packages used in our project, and help us list all the external dependencies our project requires;
-
Packages are simply a directory in our project containing one or more Go source files. We can install external packages via the
go getcommand; -
We have to always declare the executable program in our project under
package mainand all the code must be within themain()function; -
We can compile a Go program using the
go buildcommand: this will create an executable binary file named after the compiled Go source file.
Let's do some exercises now to test our knowledge about building a Go program from scratch.