My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
The proper way to install Golang and set up your workspace

The proper way to install Golang and set up your workspace

alexander ikeh's photo
alexander ikeh
·Apr 7, 2020

There are a trillion articles out there on how to install Golang and get it running on your environment but a lot of these how-to tutorials don't really follow-up the recommended, safer and faster way to setup goland on your PC and begin to build awesome application with the Language. If you dont follow golang officially recommended structure you might end up with a mixed up confused project's directory.

Firstly let us have a brief intro of what the language actually is... Golang or shortened as Go is a general-purpose programming language developed by the Google core team for building lager scale and complex software. It is a very simple language and beginner friendly. The language is statically typed, flexible and encourages modular program construction.

Lets dive into the installation!!

before we begin installation, let us first see the system requirements for our Go environment

1_KSFb47INand-6B-S8-uZwA.png

To install Golang, visit their installation page at https://golang.org and click on the download button. After this select the operation system you want to install the Go (for my case windows). 1_5PtbPMdaHEJ_vAzEUWUdRw.png

after download is complete, we locate the file on our system where our downloads are kept. We run the Go installer and follow the process for installation of Go.

1_8l12V96ORfs2fTwYukKAWQ.png

once our download is complete we can now start setting up out environment to be able to run Go in an organised directory.

now open your command prompt or terminal and type this

go version

this will tell you the current version of Go you installed and it shows your installation was successful without any errors.

All Go projects should be kept in a single workspace which is illustrated below

*go (workspace)

- bin
- src
    - github.com
        Alexswiss
           - Go project 1
                #Project files
           - Go project 2
                # project files
- pkg*

The Go language automatically creates a folder named "go" in your home directly like in my case C://User/Alexswiss/go and sets it environment variable. To check your GOPATH ; in your terminal enter

go env

scroll down and see your GOPATH. This directory can be changed to whichever you want but since we are trying to use a standard way to set up we should leave this default directory and we are fine.

okay...

now in your home directory create a folder called go. Inside go, create two folders called bin and src open the src folder and create another folder called github.com change in to the github.com directory and create a folder with your github username example alexswiss inside this directory is where all our Go code and project will be kept.

We will write a simple hello world application.. okay so we make a new directly called my_app now we will open this directly in our text editor (i use vs code) and install the Go extension create a new file main.go this is the convention for our entry point in Go. Once you finish creating the file you will find a pop up at the bottom right of your screen which will ask you to install some required plugins, go ahead and click okay, wait for installation to finish

now in your main.go type in the following code

```package main

import "fmt"

func main() { fmt.println("hello world") }```

Open your terminal, nake sure you are in the my_app directory and run

go run main.go

your application is going to output hello world

Voila! we have written our first Go application!

in my next article, i will be writing on the basic of Go and i will focus on only the necessary parts that can get you started in writing Go code for real life development.