Get Started with Go

Setup

Go can be installed from: https://go.dev/doc/install

Once installed, you can check the version:

go version

1. Project Initialization

Create a new folder for your project and move into it:

mkdir hello
cd hello

Initialize a Go module:

go mod init example/hello

This creates a go.mod file which defines your module name and will store dependency information.

2. Running a Go File

Create a main.go file with:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Run the program:

go run .

3. Installing and using Libraries

Edit main.go to use an external package:

package main

import (
    "fmt"
    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Go())
}

Tell Go to resolve and download dependencies:

go mod tidy

This will:

Run the program again:

go run .

You should see a Go-related quote printed.

4. Formatting Your Code

Go has a built-in formatter so your code style is consistent:

# Format the current package
go fmt

When Getting Back to a Project

When you return to an existing Go project:

cd hello

# Make sure dependencies are up to date
go mod tidy

# Run your program
go run .

Go will automatically download any missing dependencies defined in go.mod.