Visual Studio Code For Golang
This article helps to get started with golang using vscode.The Go extension for vscode provides many useful features such as, auto-completion, debugging,in-context help etc that makes it one of the most powerful ide’s for golang.
Install Visual Studio Code (on Mac)
To install Visual Studio Code, follow these steps
Install Go (on Mac)
There are multiple ways to install golang
1) Tarball
Download archive and extract it at location usr/local/go
.
Detailed steps are provided here
2) Installer
This option entails downloading a .pkg installer for macos and following the GUI installation wizard to install go. The steps are provided here
3) Brew
Brew is the package manager for macOS and helps in easy installtion for third party tools, libraraies and binaries.
brew update
brew install golang
After install check the go is installed properly
go version
go version go1.14.4 darwin/amd64
Your First Program
Starting with golang version 1.13
Go Module is default for development.It is new dependency management system which makes dependency information explicit and easier to manage - similar to maven, gradle in java land. With Modules, there is no need to have all code inside a single go-workspace and its possible to create modules in any directory.
VSCode Go Extension supports Go Modules and enables intellisense for imported packages by making use of language server and other necessary go-tools.VSCode will prompt to install the go extension and supported modules on detecting a .go
file but, if it doesn’t then we can install them manually.
- Install go extension
- install go tools
1) Create Module
To compile and run go hello world, we must first create a module with a module path
. Module path is the prefix for all packages of the module. Module-path is in-accordance with module’s actual physically addressable location on the web (ex.github). It can take the form like github.com/user-name/module-name for ex. github.com/lruchandani/hello
.
Following are the steps to create a module
mkdir hello-world
cd hello-world
go mod init github.com/lruchandani/hello-world
This would create a file go.mod inside hello-world
directory. The file looks like below
module github.com/lruchandani/hello
go 1.14
2) Write hello-world
Create a file hello.go inside directory hello-world
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, world.")
}
3) Open VsCode at module directory
- Open terminal
- Ensure that you are in
hello-world
directory - type
code .
. This is a shortcut to open current directory in vscode - you will get the prompts to install go extension if not already installed.
- Opt to use the language server and set essential settings in vscode to enable intellisense
"go.autocompleteUnimportedPackages": true,
"go.docsTool": "gogetdoc",
"go.gocodeAutoBuild": true,
"go.useLanguageServer": true,
"go.formatTool": "goimports",
- run the program through vscode by
^D
or by issuing following command at the terminal
go run github.com/lruchandani/hello-world
- Check that auto-complete works for third party packages