Hello, Golang!

Tanveer Hassan
3 min readJul 21, 2018

--

Setting up

Download and install Go for your environment from here.

After installation you should set the GOPATH. By default, the GOPATH is in /Users/{username}/go.

If you are using ohmyzsh like I do, open the .zshrc file in your home directory and add the following lines:

export GOPATH=”/Users/tanveer/go”
export PATH=$PATH:$GOPATH/bin

Replace my username, tanveer, with yours.

The second line is so that you can run Go commands and cli applications such as swag etc from the terminal.

Creating a project

Go follows a certain convention for project directory structures.

$ pwd
/Users/tanveer/go
$ tree -L 1
.
├── bin
├── pkg
└── src

bin contains the binaries of commands.

pkg contains the binaries of packages.

src contains the source for packages (this is where you put your project).

$ pwd
/Users/tanveer/go/src
$ tree -L 2
.
├── github.com
│ ├── golang
│ ├── gorilla
│ ├── nsf
│ ├── pkg
│ ├── ramya-rao-a
│ ├── swaggo
│ ├── tools
│ ├── urfave
│ ├── war1oc
│ └── zmb3
├── golang.org
│ └── x
├── gopkg.in
│ └── yaml.v2
├── honnef.co
│ └── go
└── sourcegraph.com
└── sqs

There are a lot more subfolders which I removed from the snippet to make it more readable. As you can see, these are repository domains. Say we push our project to Github, we’ll create the project under the github.com directory and inside the username (mine is war1oc) folder. So, I’ll create the hello-go in $GOPATH/src/github.com/war1oc/hello-go.

The Code

Create a file in the project directory and name it main.go.

The insides:

package mainimport "fmt"func main() {
fmt.Println("Hello, Go!")
}

The first line states the package in which this belongs, the application source code can be divided into packages for maintainability and reusability.

The main package tells the compiler that this is to be built as an executable program and not a library.

The second line imports the package fmt so we can use it here. The Golang standard library includes a lot of useful packages to start with such as this.

Then comes the function definition, the main function is the entry point of the main package and will be executed when running the program.

The line inside the function should be self explanatory, it just prints the string to the console.

Run the program using go run main.go

$ go run main.go
Hello, Go!

You can build the application using go build

This will spit out a binary executable file called hello-go which you can run (you might have to put it inside the bin folder).

You can also install the application directly using go install

This will build and install the application for you in the bin dir.

That’s it! That’s your first program in Golang. Super simple right? This was just a very tiny and simple example. In future articles, I will post about how to create and work on larger Golang applications such as microservices and large API applications.

Comment below if you have any questions or suggestions. You can also tweet me at @war1oc.

--

--