> For the complete documentation index, see [llms.txt](https://learn-go-with-tests.kkame.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn-go-with-tests.kkame.net/go-fundamentals/install-go.md).

# Install Go

The official installation instructions for Go are available [here](https://golang.org/doc/install).

This guide will assume that you are using a package manager for e.g. [Homebrew](https://brew.sh), [Chocolatey](https://chocolatey.org), [Apt](https://help.ubuntu.com/community/AptGet/Howto) or [yum](https://access.redhat.com/solutions/9934).

For demonstration purposes we will show the installation procedure for OSX using Homebrew.

## Installation

The process of installation is very easy. First, what you have to do is to run this command to install homebrew (brew). Brew has a dependency on Xcode so you should ensure this is installed first.

```bash
xcode-select --install
```

Then you run the following to install homebrew:

```bash
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

At this point you can now install Go:

```bash
brew install go
```

If you are going to deploy your programs to Linux based servers, you should enable cross compilation feature. If so, install using the following command:

```bash
brew install go --cross-compile-common
```

*You should follow any instructions recommended by your package manager. **Note** these may be host os specific*.

You can verify the installation with:

```bash
$ go version
go version go1.10 darwin/amd64
```

## Go Environment

Go is opinionated.

By convention, all Go code lives within a single workspace (folder). This workspace could be anywhere in your machine. If you don't specify, Go will assume $HOME/go as the default workspace. The workspace is identified (and modified) by the environment variable [GOPATH](https://golang.org/cmd/go/#hdr-GOPATH_environment_variable).

You should set the environment variable so that you can use it later in scripts, shells, etc.

Update your .bash\_profile to contain the following exports:

```bash
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```

*Note* you should open a new shell to pickup these environment variables.

Go assumes that your workspace contains a specific directory structure.

Go places its files in three directories: All source code lives in src, package objects lives in pkg, and the compiled programs live in bin. You can create these directories as follows.

```bash
mkdir -p $GOPATH/src $GOPATH/pkg $GOPATH/bin
```

At this point you can *go get* and the src/package/bin will be installed correctly in the appropriate $GOPATH/xxx directory.

## Go Editor

Editor preference is very individualistic, you may already have a preference that supports Go. If you don't you should consider an Editor such as [Visual Studio Code](https://code.visualstudio.com), which has exceptional Go support.

To install VS Code using brew, because this is a GUI application you need an extension to homebrew called cask to support install VS Code.

```bash
brew tap caskroom/cask
```

At this point you can now use brew to install VS Code.

```bash
brew cask install visual-studio-code
```

You can confirm VS Code installed correctly you can run the following in your shell.

```bash
code .
```

VS Code is shipped with very little software enabled, you can enable new software by installing extensions. To add Go support you must install an extension, there are a variety available for VS Code, an exceptional one is [Luke Hoban's package](https://github.com/Microsoft/vscode-go). This can be installed as follows:

```bash
code --install-extension ms-vscode.go
```

When you open a Go file for the first time in VS Code, it will indicate that the Analysis tools are missing, you should click the button to install these. The list of tools that gets installed (and used) by VS Code are available [here](https://github.com/Microsoft/vscode-go/wiki/Go-tools-that-the-Go-extension-depends-on).

## Go Debugger

A good option for debugging Go (that's integrated with VS Code) is Delve. This can be installed as follows using go get:

```bash
go get -u github.com/derekparker/delve/cmd/dlv
```

## Go Linting

An improvement over the default linter can be configured using [Gometalinter](https://github.com/alecthomas/gometalinter).

This can be installed as follows:

```bash
go get -u github.com/alecthomas/gometalinter
gometalinter --install
```

## Refactoring and your tooling

A big emphasis of this book is around the importance of refactoring.

Your tools can help you do bigger refactoring with confidence.

You should be familiar enough with your editor to perform the following with a simple key combination:

* **Extract/Inline variable**. Being able to take magic values and give them a name lets you simplify your code quickly
* **Extract method/function**. It is vital to be able to take a section of code and extract functions/methods
* **Rename**. You should be able to confidently rename symbols across files.
* **go fmt**. Go has an opinioned formatter called `go fmt`. Your editor should be running this on every file save.
* **Run tests**. It goes without saying that you should be able to do any of the above and then quickly re-run your tests to ensure your refactoring hasn't broken anything

In addition, to help you work with your code you should be able to:

* **View function signature** - You should never be unsure how to call a function in Go. Your IDE should describe a function in terms of its documentation, its parameters and what it returns.
* **View function definition** - If it's still not clear what a function does, you should be able to jump to the source code and try and figure it out yourself.
* **Find usages of a symbol** - Being able to see the context of a function being called can help your decision process when refactoring.

Mastering your tools will help you concentrate on the code and reduce context switching.

## Wrapping up

At this point you should have Go installed, an editor available and some basic tooling in place. Go has a very large ecosystem of third party products. We have identified a few useful components here, for a more complete list see <https://awesome-go.com>.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn-go-with-tests.kkame.net/go-fundamentals/install-go.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
