Introduction to go

Go is a statically typed programming language that makes it easy to build simple, reliable, and efficient software. Developed by google in 2007, go has seen immerse adoption from companies all over the world in creating different types of application from cloud computing, command-line interfaces, web development to development operations & site reliability engineering.

Installing go

Go is very straight forward to install and start programming software. Follow the instruction for your operating system at this link.

If you have completed the installation, to verify go is installed on your system open command prompt, cmd if you are on windows OS and your terminal if using linux type this command go version it should print the version of go. Alt text

Creating a go program

Go programs are usually created in a module within any part of your system. Assuming we are in Desktop location within our system, let’s create a folder that will hold our go program called “task-manager” and run some commands to create a module

  • mkdir task-manager
  • cd task-manager
  • go mod init "task-manager" (this last word is the name of your module and the path, this is usually a part for git repository that can be accessed externally)

The above command will create a file go.mod which means our folder “task-manager” is ready hold go programs. Alt text

A go program is a collection of several packages with a main package which serves as the entry point of the program. the main package with a main function is the minimum requirement to create a go program.

Every go file must end with a .go extension after the name of the file. Lets create a file called main.go within out task-manager folder. Open the created file with a text editor e.g vscode and type the following code.

package main

import "fmt"

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

The first line declared that the file belongs to a package called “main”.
The second line is bringing a package fmt from the standard library into this file to be used.
The third line is declaring a function called “main” which is the entry point of our program within the main function we are using the imported package fmt calling Println function with an argument(“Hello world!") Alt text

Running a go program

Now lets run our program by opening our command prompt in our task-manager folder location, type the code go run main.go and press enter. we should be able to see “Hello world!” printed as an output. Alt text

Building a go program

In the above section we looked at a command go run which is used to compile and run go program.but this will not create a binary, to create a binary we will use the command go build this should create a binary “task-manager” which when executed will output “Hello world!” Alt text

If you have followed through to this point, congratulations on your first journey into programming using the go language and building your first program.