- Install homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Update homebrew.
brew update
- Install Go.
brew install go
- Verify version.
go version
- Add the following lines to your shell profile. Open
~/.bash_profile
or~/.zshrc
(depending on your system) with VS Code to set theGOPATH
and include Go binaries in yourPATH
.
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Setting up a comprehensive Go development environment in Visual Studio Code (VS Code) enhances your productivity by providing features like automatic formatting, linting, debugging, and more.
-
Launch Visual Studio Code on your machine.
-
Click on the Extensions icon in the Activity Bar on the side of VS Code.
-
Alternatively, use the keyboard shortcut:
⇧⌘X
on macOSCtrl+Shift+X
on Windows/Linux
-
Search for "Go"
- In the Extensions marketplace search bar, type
"Go"
.
- In the Extensions marketplace search bar, type
-
Install the Official Go Extension
-
Reload VS Code
- After installation, you may be prompted to reload VS Code.
- Click Reload to activate the extension.
The Go extension relies on various tools to provide features like IntelliSense, formatting, linting, and debugging. You can install these tools automatically or manually.
-
Open the Command Palette
- Press
⇧⌘P
(macOS) orCtrl+Shift+P
(Windows/Linux).
- Press
-
Run the Install Tools Command
- Type
Go: Install/Update Tools
and select it.
- Type
-
Select Recommended Tools
- The extension will suggest a list of tools. Commonly recommended tools include:
gopls
(Language Server)goimports
(Formatting and Import Management)golangci-lint
(Linting)dlv
(Debugger)staticcheck
(Advanced Linter)gomodifytags
(Struct Tag Manipulation)
- The extension will suggest a list of tools. Commonly recommended tools include:
-
Install Selected Tools
- Select all recommended tools and click OK to begin installation.
Proper configuration of environment variables ensures that VS Code and Go tools work seamlessly. You can adjust settings either through the GUI or by directly editing the settings.json
file.
-
Open
settings.json
- Press
⇧⌘P
(macOS) orCtrl+Shift+P
(Windows/Linux) to open the Command Palette. - Type
Preferences: Open Settings (JSON)
and select it.
- Press
-
Add or Modify the Following Configuration
{
{
// Global Settings
"go.gopath": "/Users/alexis/go",
"go.goroot": "/usr/local/go",
"go.formatTool": "goimports",
"editor.formatOnSave": true,
"go.lintTool": "golangci-lint",
"go.lintOnSave": "file",
"go.lintFlags": ["--fast"],
"go.vetOnSave": "package",
"go.coverOnSave": true,
"go.testOnSave": true,
"go.useLanguageServer": true,
"go.languageServerFlags": ["-rpc.trace", "serve"],
"go.toolsManagement.autoUpdate": true,
// Editor Settings for Go
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
},
"editor.defaultFormatter": "golang.go"
},
// Go Language Server (gopls) Settings
"gopls": {
"analyses": {
"unusedparams": true
},
"codelenses": {
"generate": true,
"gc_details": true,
"test": true
},
"formatting.gofumpt": false
},
// other settings
}
Happy coding!
package greeting
// HelloWorld greets the world.
func HelloWorld() string {
return "Hello, World!"
}
Go is a statically typed, compiled programming language. Go is syntactically similar to C. The language is often referred to as Golang because of its previous domain name, golang.org, but the proper name is Go.
The Basics concept will introduce three major language features: Packages, Functions, and Variables.
-
Go applications are organized in packages. A package is a collection of source files located in the same directory. All source files in a directory must share the same package name.
-
It is conventional for the package name to be the last directory in the import path. For example, the files in the "math/rand" packagebegin with the statement
package rand
. -
When a package is imported, only entities (functions, types, variables, constants) whose names start with a capital letter can be used / accessed.
-
The recommended style of naming in Go is that identifiers will be named using
camelCase
, except for those meant to be accessible across packages which should bePascalCase
.
package lasagna
-
Go is statically-typed, which means all variables must have a defined type at compile-time.
-
Variables can be defined by explicitly specifying a type:
var explicit int // Explicitly typed
- You can also use an initializer, and the compiler will assign the variable type to match the type of the initializer.
implicit := 10 // Implicitly typed as an int
- Once declared, variables can be assigned values using the
=
operator. Once declared, a variable's type can never change.
count := 1 // Assign initial value
count = 2 // Update to new value
count = false // This throws a compiler error due to assigning a non `int` type
-
Constants hold a piece of data just like variables, but their value cannot change during the execution of the program.
-
Constants are defined using the
const
keyword and can be numbers, characters, strings or booleans:
const Age = 21 // Defines a numeric constant 'Age' with the value of 21
-
Go functions accept zero or more parameters. Parameters must be explicitly typed, there is no type inference.
-
Values are returned from functions using the
return
keyword. -
A function is invoked by specifying the function name and passing arguments for each of the function's parameters.
package greeting
// Hello is a public function.
func Hello (name string) string {
return hi(name)
}
// hi is a private function.
func hi (name string) string {
return "hi " + name
}
- Note that Go supports two types of comments. Single line comments are preceded by
//
and multiline comments are inserted between/*
and*/
.