DEV Community

Ko Takagi
Ko Takagi

Posted on

How to setup Golang with VSCode

I have used Golang some years ago, but unfortunately I hadn't tried to do it in a long time and had forgotten all about it....

So I set it up from the beginning with a fresh mind.😊

The environment I used is as follows.

  • macOS Catalina 10.15.5
  • Visual Studio Code 1.46.1

Installing Go

Download and install Go from the following link.

Go

After the installation, open a terminal and check the version using the following command.

$ go version
go version go1.15.2 darwin/amd64
Enter fullscreen mode Exit fullscreen mode

Create directory for GOPATH setting.

$ mkdir ~/go
Enter fullscreen mode Exit fullscreen mode

For GOPATH envirionment variable, add the following lines to the ~/.bash_profile.

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

Installing Visual Studio Code

Download and install VSCode from the following link.

After the installation, Launch VSCode.

Open the Extensions Marketplace (Cmd+Shift+X), search Go and install it.

Go extension

open the Command Palette (Cmd+Shift+P) and run the Go: Install/Update Tools command.

Go extensions

Install all the Go extensions listed there.

Go extensions

Start coding

Create hello.go in VSCode.

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
Enter fullscreen mode Exit fullscreen mode

By default, auto-completion and formatting for code saving are enabled. That's great.

auto-completion

Open a terminal (Ctrl+`) and execute the following command.

$ go run hello.go 
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Have fun!👍

Discussion (11)

Collapse
jjamieson1 profile image
Jamie Jamieson

Thank you! Easy start for me

Collapse
ko31 profile image
Ko Takagi Author

You're welcome!

Collapse
pedromassango profile image
Pedro Massango

Thanks for saving me today. I already spent 3h on this

Collapse
n1ru4l profile image
Laurin Quast

Exactly what I needed 🚀

Collapse
ko31 profile image
Ko Takagi Author

Thanks! 😊

Collapse
mngt profile image
Aman Gautam

Great Articles!

Collapse
jhoval profile image
Mauro Franchini

Thanks! Fast and straight forward.

Collapse
velbie profile image
velbie

Thank you Takagi!

Collapse
ko31 profile image
Ko Takagi Author

Thanks! 👍

Collapse
spiropoulos94 profile image
NikosSp

all good, but I have no curly lines error highlighting. anyway to fix this?

Collapse
xleki profile image
xleki

How do I have the little play button in the top right like in python