Last reviewed
Install
Download and install the Go distribution for your platform from the official website. This will install the Go compiler and tools on your system.
You don't need to set GOPATH or create a special workspace directory. Go modules are the default, so a project can live in any directory. Go still uses
$HOME/goby default for downloaded modules and for tools you install.(Optional) Add
$HOME/go/binto your PATH so that you can run tools installed withgo installfrom any directory. On macOS or Linux, addexport PATH="$PATH:$HOME/go/bin"to your shell config file (.zshrcor.bashrc). On Windows the location is%USERPROFILE%\go\bin.Test your installation by opening a terminal or command prompt and running
go version. If Go is installed correctly, you should see the version number.
Setting up a new Go project
- create a new directory for your project
mkdir newproj - change into that directory.
cd newproj - initialize the module with
go mod init example.com/newprojThis will create a go.mod file in your project directory, which defines the dependencies of your project. The module path is required, and is normally where the code will live, such asgithub.com/yourname/newproj. - create a new file with a go extention (main.go by convention)
- write your code
- run it with
go run .or build a binary withgo build
for example:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}

