Dev In The Mountain Header
A Developer In The mountains having fun

Last reviewed

Install

  1. Download and install the Go distribution for your platform from the official website. This will install the Go compiler and tools on your system.

  2. 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/go by default for downloaded modules and for tools you install.

  3. (Optional) Add $HOME/go/bin to your PATH so that you can run tools installed with go install from any directory. On macOS or Linux, add export PATH="$PATH:$HOME/go/bin" to your shell config file (.zshrc or .bashrc). On Windows the location is %USERPROFILE%\go\bin.

  4. 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

  1. create a new directory for your project mkdir newproj
  2. change into that directory. cd newproj
  3. initialize the module with go mod init example.com/newproj This 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 as github.com/yourname/newproj.
  4. create a new file with a go extention (main.go by convention)
  5. write your code
  6. run it with go run . or build a binary with go build

for example:

go
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
More places to find me
Mental Health
follow me on Mastodon