DEV Community

Cover image for A gentle introduction to the Go tool chain
Amin
Amin

Posted on

A gentle introduction to the Go tool chain

After this introduction, you'll be able to run any Go programs on your local filesystem. This is not an introduction to the language but more on the tools that are used to help you ease the beginning of your Go journey.

Go is a multi-purpose programming language that can build to a single executable that you can use almost anywhere for tasks such as Web development, system programming or GUI desktop applications.

It is a pretty easy language to pick that has a solid set of tools that can help you build reliable and robust programs.

Why

You want to quickly be productive in writing Go programs and want a gentle introduction on the tool chain to use for testing make your first dive into the Go programming language.

Who

You are a Web developer and are searching for an alternative to Node, PHP, Ruby. Especially a compiled language alternative.

You are a system programming and want to test out an alternative programming language for writing command-line interfaces or graphical programs.

You are simply curious and want to extend your range of skills.

What

This article will focus on the Go installation and setup. This is aimed to help you setup up your first Go environment to follow along with the official documentation.

Installation

You will need to install the Go programming language for your operating system if you want to get started. I'll let you follow along the tutorial here since covering all operating systems would take too much time.

Commands in this article will be based on a GNU/Linux operating system. If you are on Windows, I suggest you search and find alternatives for each commands in this article. I'm pretty sure there should be nothing that you can't do on Windows as on GNU/Linux, but I'll let you do your own researches since I don't own a Windows machine.

If the installation is successful, you should get an output for the following command.

$ go version
go version go1.14.4 linux/amd64
Enter fullscreen mode Exit fullscreen mode

Folder architecture

First, we will create a namespace for our own work. Since you'll probably be using the work of other people (if you choose to continue on the Go path), you'll need to separate the concerns.

$ mkdir -p $HOME/go/src/github.com/yourusername
Enter fullscreen mode Exit fullscreen mode

There is no concept of relative imports in Go. Go will always look up library to include in the standard library, or in the $GOPATH/src folder (there is also another folder where Go will look up for installed third-party library but I won't bother you with it now). If the $GOPATH environment variable is not set, it will default to $HOME/go.

And by the way, you can use any namespace you want. This is just a convention if you are a GitHub user. But you could of course choose something else if you are using GitLab for instance.

$ mkdir -p $HOME/go/src/gitlab.com/yourusername
Enter fullscreen mode Exit fullscreen mode

We should now move our current directory in the created folder.

$ cd $HOME/go/src/github.com/yourusername
Enter fullscreen mode Exit fullscreen mode

Now that we are set, we can start writing some Go programs. Let's create the folder for our new project first.

$ mkdir simplemath
$ cd simplemath
Enter fullscreen mode Exit fullscreen mode

We will write a simple library for managing simple math operations. Let's create the source file.

$ touch main.go
Enter fullscreen mode Exit fullscreen mode

And now for the content of this file.

package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    var a int = 1
    var b int = 2

    fmt.Printf("%d + %d = %d.\n", a, b, add(a, b))
}
Enter fullscreen mode Exit fullscreen mode

Don't be afraid if you don't understand a dime about this code. This is just an example to help you run your first Go program.

But since the Go programming language is very easy to pick, and if you are coming from a language like Java, JavaScript or C, you should have little to no problems to understand what this code snippet does.

Now let's run our first program.

$ go run main.go
1 + 2 = 3.
Enter fullscreen mode Exit fullscreen mode

If you successfully managed to get the same output, congratulations!

Guess what? You can also run your program by using its namespace.

$ go run github.com/yourusername/simplemath
1 + 2 = 3.
Enter fullscreen mode Exit fullscreen mode

And if you asked: we also can download other people's Go sources and use them on our local filesystem. This is one of the reason why we created this namespace (to prevent clashing with others people's similar programs) but also for organization purposes.

Conclusion

You are now capable of installing Go on your favorite operating system. You are also able to run example programs by writing their source-code on your local filesystem.

What's up next?

You can take a look at A Tour of Go which is a website tutorial on getting started with the Go programming language syntax and standard libraries.

I highly suggest you follow along this tour by writing the programs on your own local filesystem. This will get you used to writing Go programs.

Though I'm a complete beginner Go developer, there are many smart people here on DEV that can help you if you get stuck. Don't hesitate to ask questions down below!

Latest comments (3)

Collapse
 
s0xzwasd profile image
Daniil Maslov

Good article! Thanks!

Collapse
 
kevinschweikert profile image
Kevin Schweikert • Edited

You don't need to put your sourcefiles in the $GOPATH. You can create a folder anywhere and use Go Modules with go mod init github.com/yourusername for example

Collapse
 
aminnairi profile image
Amin

Thanks for your answer. This sure looks easier that way. Can you still include this project on others folders of your local filesystem as well?