Go Programming Tutorial

  • Teacher
  • 38 (Registered)
  • (0 Reviews)

Go Programming Tutorial

Configuring your Go development environment

In this lesson we will learn about the Go development environment and we will configure our development system accordingly.

Go Workspace

When you installed Go, an environment variable GOPATH was set that points to the Go workspace.

GOPATH typically points to $HOME/go.

We need to make sure that we have src and bin directories.

Go is very particular about where these directories are.

If you want to have your Go workspace in a different location then make sure that you update the GOPATH environment variable accordingly.

All of the Go commands and sub commands depend on knowing where the GOPATH is located at.

Source Folder Organization

In the go/src folder, we organize Go source code in a particular way.

Go is tightly integrated with git and the organization of this folder reflects that.

Under the src folder are folders representing git repositories.

As an example, here is my src folder

And here is a tree view of the directories:

.
├── git.admintome.com
│   ├── goroutines
│   ├── hello
│   └── manageGameData
├── git.admintome.lab
│   ├── goroutines
│   ├── hello
│   └── manageGameData
├── github.com
│   ├── acroca
│   ├── derekparker
│   ├── karrick
│   ├── kubernetes-incubator
│   ├── mdempsky
│   ├── pkg
│   ├── ramya-rao-a
│   ├── robertkrimen
│   ├── rogpeppe
│   ├── sqs
│   └── uudashr
└── golang.org
    └── x

As you can see, the src directory has a very specific organization structure and all the go command line tools rely on this structure to function correctly.

Create a new subdirectory under git.admintome.com called go-programming-tutorial.

Writing our first application

Like every other programming tutorial out there we will be writing a Hello World program.

In your terminal, go to the src directory in your go workspace.

Start VS Code.

code .

Right-click on an empty space in the Explorer window and click on New Folder.

Name the folder github.com or whatever you use for your git repositories.

You will see the folder in the list of folders in the Explorer window.

Right-click that new folder and select New Folder again.

Name it hello.

Right-click your new hello folder and click on new file.

Name it main.go.

VS Code will now open the file in the editor window.

Add the following code:

-> git.admintome.com/go-programming-tutorial/hello/main.go

package main

import (
  "fmt"
)

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

Click on File -> Save.

We now need to compile and run our new program.

Compiling our Code

For this lesson, we will compile and run our code using the command line.

In later lessons, we will be using VS Code to compile and run our programs.

In your terminal change to the hello directory.

GOPATH/src/github.com/hello

To compile our program we use the build sub command.

go build main.go

This will compile our source code into a binary and write the binary file to the current directory.

Run your program

You should now see a hello (or hello.exe if you are on Windows) file in your directory.

Run this file.

$ ./hello 
Hello, World!

We can also build and run using the run sub command:

$ go run main.go
Hello, World!

 

Try it out in the Go Playground.

No comments yet! You be the first to comment.

Leave a Reply

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