Setting Up Dev Environment for Golang
Tags: go, vscode, productivity
Published: 2020-11-19 06:43:58

In this post, we will be setting up our development environment for writing go-code. Now, first thing that should be clear is having a good development environment as it brings up your "A-game" when writing good quality codes. The better your environment, the less work you have to do, and the better the code quality. **Do Less, Do Better**.

Assuming you have no prior experience in installing Go, we will first start by installing Go in Windows and then in Ubuntu(can be applied to any other distro). For Mac, I have no experience in installing Go in Mac, so sorry folks but you have to work on yourself and then join us later when setting up *VSCode*.

## For Windows
---

First of all, head over to [golang.org](https://golang.org/ "Golang's Official Website") and **click on** "Download Go" button. You will be redirected to a new page. Now click on the .msi installer under the Microsoft Windows box, the installer will be downloaded. Once, the installer is downloaded, open it and install Go. (Just do next, next, next!). Once Go is installed, you can verify it by opening your **Command Prompt** and enter 
```
go version
```
![Go Version is shown](https://dev-to-uploads.s3.amazonaws.com/i/pv8o0gmlf33710l27vg8.png)

### Next step is creating a workspace folder. 
On your PC, create a folder which will be used to store Go Projects. Mine is in Local Disk C. You can see the path.
```
C:\workspace\go
```
Now create three directories in here by the name **bin**, **pkg**, **src**. The **src** folder is the one which we will for our projects initially before learning about **Go Mod**.

After creating a workspace folder, we have to add it to the GOPATH. To do this, first search for environment variables in your search window. (Search Window can be open by pressing Win+S). 
Click on edit environment variables for your account. 

Now, under user variables, i.e. the first dialog box, if no **GOPATH** variable is there, then create a new one by clicking on New. Then under variable name, write **GOPATH** and under variable value, copy the path of your workspace folder.

Mine looks something like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8bs2n76j15ofo9xoajgk.png)

That's it, you have successfully installed Go in your Windows PC. You can now jump directly to the VSCode part of the post.
---

# For Ubuntu 
---

Firstly, we will download the tar ball of Latest Go version. At the writing of this post, the latest Go Version is 1.15.something something. You can check the latest version by going to the official golang website [golang.org](https://golang.org/ "Golang's Official Website"). Click on **Download Go** and check the latest version. You can download it from there, or you can follow the way I am showing. The reason is that those who are running Ubuntu on WSL (like me), for them downloading and then moving the Go Tar file is kind of a boring work. 

In your terminal, write the following term.
```
wget -c https://dl.google.com/go/<insert your version here>.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
```
The above step will download the tar file, will extract it and place the resultant directory under **/usr/local/**. 

Now, we have to set the **GOPATH**. In your **.profile** file, located in your root, place the following line at the end of the file.
```
export PATH=$PATH:/usr/local/go/bin
```

By default, the GOPATH is set us **$HOME/go**. Therefore, in your HOME directory, you have to create a directory named go. You can do this by copying and pasting the following text.
```
mkdir ~/go
```

Now create three directories in here by the name **bin**, **pkg**, **src**. The **src** folder is the one which we will for our projects initially before learning about **Go Mod**.

Now, source your .profile file or just restart your terminal.
Verify your installation by writing ```go version``` in your terminal.

---


# Setting Up VSCode
---
I use VSCode as my code editor but feel free to use any other code editor of your choice. If you are using VSCode, then follow the steps.

1. Install VScode on your PC. 
1. Download the official go-extension. 
1. Once, the extension is installed, a pop-up will be shown to download some additional tools. Make sure you click on **Install All**.
1. Next step is setting up an advanced **Linter**. Open your settings.json file by pressing **CTRL + ,** and on top right, click on the file icon.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/68lii34064v4krqdtoc4.png)
1. Copy the following text in the end of the file.
```
"go.lintTool":"golangci-lint",
"go.lintFlags": [
  "--fast"
]
```
Make sure you format this document otherwise it may break your VSCode.
---

### So that's it for this post, we will start working on learning Go by solving real world problems from the next post. It is already publish on **Medium** prior to writing this post. You can check out those posts also.

These are the friend links:

* [Learn Go by solving real world problems](https://medium.com/golang-jedi-knight/learn-golang-by-solving-real-world-problems-955c609ff0db?source=friends_link&sk=7566d31760ca0995cc6dac306b43bb6b)

* [Primitive Data Types in Go](https://medium.com/golang-jedi-knight/primitive-data-types-in-golang-35a291df3bbe?source=friends_link&sk=737a6bfb6776ff330fd4dbbcd5da100e)

* [Zero Values and Complex Data Types in Go](https://medium.com/golang-jedi-knight/zero-values-and-complex-data-types-in-golang-20ec177d11a2?source=friends_link&sk=9376caa0a5e769945027b46dac6730ad)

**You can contact me on twitter [@neel229](https://twitter.com/neel229)** ✌