Go Tutorial · Go Basics · Lesson 2 of 30

Go Get Started

Install Go, create a module and run your first program.

Install Go

Download the installer from go.dev/dl, then check it:

go version

Create a module

A module is a Go project with a go.mod file that records its name and dependencies:

mkdir hello && cd hello
go mod init example.com/hello

Create main.go:

Example 1
package main

import "fmt"

func main() {
	fmt.Println("Hello from my first Go module!")
	fmt.Println("Go makes", 1+1, "things easy")
}

Output

Hello from my first Go module!
Go makes 2 things easy

Run this on your computer — see “Get Started” for setup.

Run and build

go run .          # compile and run
go build          # produce a binary named hello
./hello

Other everyday commands: go fmt ./... (format code — every Go project uses it), go vet ./... (find suspicious code), go test ./... (run tests) and go get <module> (add a dependency).

Test yourself

Which command creates a go.mod file?

  1. go new
  2. go mod init
  3. go create
  4. go init mod
Show answer

Answer: go mod init