Skip to content

alnah/vs-code-go-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

OS Installation

  1. Install homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Update homebrew.
brew update
  1. Install Go.
brew install go
  1. Verify version.
go version
  1. Add the following lines to your shell profile. Open ~/.bash_profile or ~/.zshrc (depending on your system) with VS Code to set the GOPATH and include Go binaries in your PATH.
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

VS Code setup

Install Go extension for VS Code

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 macOS
    • Ctrl+Shift+X on Windows/Linux
  • Search for "Go"

    • In the Extensions marketplace search bar, type "Go".
  • Install the Official Go Extension

    • Locate the Go extension by the Go team (golang.Go).
    • Click Install. Go Extension
  • Reload VS Code

    • After installation, you may be prompted to reload VS Code.
    • Click Reload to activate the extension.

Install Go tools for VS Code via Go 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) or Ctrl+Shift+P (Windows/Linux).
  • Run the Install Tools Command

    • Type Go: Install/Update Tools and select it.
  • 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)
  • Install Selected Tools

    • Select all recommended tools and click OK to begin installation.

Edit configuration file for VS Code

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) or Ctrl+Shift+P (Windows/Linux) to open the Command Palette.
    • Type Preferences: Open Settings (JSON) and select it.
  • 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!

Basics

package greeting

// HelloWorld greets the world.
func HelloWorld() string {
	return "Hello, World!"
}

About Basics

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.

Packages

  • 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 be PascalCase.

package lasagna

Variables

  • 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

  • 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

Functions

  • 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
}

Comments

  • Note that Go supports two types of comments. Single line comments are preceded by // and multiline comments are inserted between /* and */.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages