DEV Community

Cover image for Setting Up Dev Environment for Golang
Neel Modi
Neel Modi

Posted on • Updated on

Setting Up Dev Environment for Golang

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 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
Enter fullscreen mode Exit fullscreen mode

Go Version is shown

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
Enter fullscreen mode Exit fullscreen mode

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

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. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
  2. Download the official go-extension.
  3. Once, the extension is installed, a pop-up will be shown to download some additional tools. Make sure you click on Install All.
  4. 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
  5. Copy the following text in the end of the file.
"go.lintTool":"golangci-lint",
"go.lintFlags": [
  "--fast"
]
Enter fullscreen mode Exit fullscreen mode

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:

You can contact me on twitter @neel229

Top comments (7)

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Thanks. I want to try that extension setup.

Did you know you can use apt? This makes it easier to upgrade without downloading a version of go yourself.

sudo apt install golang-go
Enter fullscreen mode Exit fullscreen mode

And either way, setup access to installed packages

export PATH="$HOME/go/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

I don't think you need mkdir step - doesn't go create that for you when you install a package?

Collapse
 
neel229 profile image
Neel Modi

Well I was not aware of this, so it is nice of you to lay it out for others.

I don't think so it creates the go directory itself for the process I have shown.

Collapse
 
michaelcurrin profile image
Michael Currin

Any reason why you chose golangci-lint over the default golint ?

Collapse
 
neel229 profile image
Neel Modi

First of all, it is faster than golint.
Second, you can append //nolint to the end of the line for the code you do want to perform linting on.

For more use cases, you can check them out here.
golangci-lint.run/usage/linters/

Collapse
 
michaelcurrin profile image
Michael Currin

in your termnial.

Another typo. Also the text looks like a heading rather than paragraph.

Collapse
 
neel229 profile image
Neel Modi

Thanks for pointing that out, it has been fixed.

Collapse
 
neel229 profile image
Neel Modi

Hey guys the Medium Blogs have been added to DEV