Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. [Setup] Go Development Environment

[Setup] Go Development Environment

This page walks you through setting up a Go development environment. You can download an installer from the official website and start developing right away.

Installation

  1. Download the installer for your OS from go.dev.
  2. Run the installer and follow the on-screen instructions.
  3. Run the following in Terminal (or Command Prompt) to verify the installation.
go version

If version information is displayed, the installation is complete.

On macOS, you can also install Go via Homebrew.

brew install go

GOPATH and Module Mode

In older versions of Go, all source code had to be placed under a directory called GOPATH. Today, module mode is the standard, and you can create projects anywhere you like.

GOPATH is still used as the storage location for downloaded packages, but you rarely need to think about it during development.

Creating and Running a Project

1. Initialize a project

mkdir hello
cd hello
go mod init hello

go mod init initializes a module (project) and creates a go.mod file.

2. Create the source file

Use a text editor to create a file named main.go with the following content.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
    fmt.Println("The Go environment is set up successfully.")
}

3. Run

go run main.go

If Hello, World! and The Go environment is set up successfully. are displayed, you're all set.

Building (Compiling)

Use go build to generate an executable file.

go build -o hello
./hello

Go also supports cross-compilation. You can generate an executable for a different OS by setting environment variables as shown below.

Example: generating a Windows executable on macOS/Linux:

GOOS=windows GOARCH=amd64 go build -o hello.exe

Commonly Used go Commands

CommandDescription
go run filename.goCompiles and runs in one step.
go buildGenerates an executable file.
go mod initInitializes a new module (project).
go mod tidyRemoves unused dependencies and adds missing ones.
go fmtFormats code to the Go standard style.
go testRuns tests.

If the Command Is Not Found

If your terminal displays go: command not found, the PATH may not be configured correctly. Follow the steps below to check and fix the issue.

1. Find the command location

Check where the command is located.

which go

If not found, check common installation locations.

ls /usr/local/go/bin/go
ls /opt/homebrew/bin/go

2. Check which shell you are using

echo $SHELL

If /bin/zsh is shown, edit ~/.zshrc; if /bin/bash is shown, edit ~/.bashrc.

3. Add to PATH

Once you know the command location, add the PATH to your shell configuration file.

For macOS (zsh):

echo 'export PATH="/usr/local/go/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

For Linux (bash):

echo 'export PATH="/usr/local/go/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

About GOPATH and GOROOT

Go has two important environment variables. They are usually configured automatically, but check them if you encounter problems.

VariableDescriptionDefault
GOROOTThe Go installation directory./usr/local/go
GOPATHThe storage location for downloaded packages and executables installed with go install.~/go

Check the current settings.

go env GOROOT
go env GOPATH

To use commands installed with go install, also add GOPATH/bin to your PATH.

For macOS (zsh):

echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

For Linux (bash):

echo 'export PATH="$HOME/go/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

For Windows, go to "Advanced System Settings" → "Environment Variables" → "Path" to add the entry. The official installer sets this automatically.

If you find any errors or copyright issues, please .