GoLang logo

The Go App

This is the part of the article where I will go into a lot more detail.

At the time of writing this paragraph I have never written a Go app, have no idea about best practices or how the language works properly.

The aim, at the end of this section, is to have a good understanding of the basics, document my learning curve and hopefully help others doing the same.

Installation

If you haven’t already, head on over to the Go Downloads page and download/install it.

One thing the installer won’t make clear is workspaces. In fact it won’t even ask you what you think about them.

Workspaces

“What the hell is a Go Workspace?” I hear you ask. The short and simple answer is it’s where all of your Go development happens.

We’re not going to change where our Workspace is (if you want to you can) so it defaults to $HOME/go . Any new projects we now start will be done inside $HOME/go/src .

If you have decided to specify a different location for your Go Workspace, you’ll need to make a src directory.

Project Setup

Now we have our workspace setup, we can create our first project. Each of our subdirectories inside the src directory will represent a separate package or command, and each will contain at least one .go file.

To get started we’ll make a simple Hello World package:

mkdir src/helloWorld (makes a helloWorld directory under src )

(makes a directory under ) cd src/helloWorld

touch main.go (creates a blank file called main.go )

Now we have our first Go programme ( main.go ), we should write some code. Open the file up in a text editor and lets add the following code:

q

Nothing fancy. We’re naming our package ( main ), importing a package ( fmt ) and then using it to print “Hello World” to the console.

When you have created this file, you can run the following commands:-

go run main.go (runs the package)

(runs the package) go build (creates a standalone executable that you can run without the Go tool)

(creates a standalone executable that you can run without the Go tool) go install (same as go build , but it places the executable in the workspaces /bin directory so you can run it from anywhere in your filesystem*)

(same as , but it places the executable in the workspaces directory so you can run it from anywhere in your filesystem*) For this to work your PATH must include your workspaces /bin directory.

Have a play

After making the “Hello World” app, I decided to take 10 minutes to have a play around with the language to see what I could learn.

imports are relative to the src directory

If you have your helloWorld package in src/helloWorld , and you decide to create a sub-package in src/helloWord/anotherPackage , the import statement is relative to the src directory so import “helloWorld/anotherPackage”

Files in the same directory can be used as part of the same package if they have the same name

If, under the helloWorld package, you have a file main.go with the following code

package main

import "fmt"

func main() { fmt.Println("Hello World", privateHelperFunc()) }

And another file anotherfile.go with the following code

package main

func privateHelperFunc() int { return 25 }

The main.go file will have access to the privateHelperFunc function as they act as though they are part of the same package.

Private functions

Functions that start with a lowercase letter (e.g. privateHelperFunc ) are private and cannot be used outside the package.

Public functions

Functions that start with a uppercase letter (e.g. PublicHelperFunc ) are public and are exported for use.

Assigning imported packages an alias

When importing a package you can alias that package. In the below example, the fmt package is aliased as notfmt :

package main import (

notfmt "fmt"

) func main() {

notfmt.Println("Hello World")

}

Three folders

In your Go Workspace you will have three folders: