01 — Install Go and write your first program

Goal

Install the Go toolchain, create a module, run a program, and learn the edit–format–run loop.

Step 1: Install and verify Go

Install Go from the official Go downloads page. Open a new PowerShell terminal and check:

go version
go env GOROOT GOPATH

You should see a Go version and two paths. If PowerShell says go is not recognized, reopen the terminal after installing or add the Go bin directory to PATH.

Step 2: Create the course project

mkdir field-notes
cd field-notes
go mod init github.com/example/field-notes

go.mod at the repository root identifies the course module and tracks dependencies. The module path is also the prefix used by Go imports. Keeping one module at the root lets Go import generated code from gen/go and server code from server/.

Step 3: Write a program

Create main.go in the repository root:

package main

import "fmt"

func main() {
    fmt.Println("Field Notes server is starting")
}

Run and format it:

gofmt -w main.go
go run .

Expected output: Field Notes server is starting.

What each line means

package main makes this an executable package. import "fmt" makes the standard formatting package available. func main() is where the program starts. fmt.Println writes a line to standard output.

Why Go uses modules and packages

Go source is organized into packages: related files in one directory normally share a package name. A module is a collection of packages that share dependency and version information in go.mod. go run compiles and runs a package; go build creates an executable; gofmt applies the standard formatting so teams don't spend time debating whitespace. The module path gives packages stable import names, even before they are published.

As the course grows, the root module will contain packages under server/ and generated protocol types under gen/go/. Initializing the module at the repository root now avoids a second nested module later.

Step 4: Build a binary

go build -o field-notes-server.exe .
./field-notes-server.exe

Building compiles the package into an executable. go run is convenient while learning; built binaries are useful for deployment.

Checkpoint

Change the printed message, run gofmt -w main.go, and run go run . again. Explain the roles of go.mod, main.go, and gofmt.

Practice

  1. Print two lines.
  2. Add a comment describing the program.
  3. Run go help mod and find the command that downloads dependencies.

Troubleshooting

  • go: go.mod file not found: run the command from the repository root.
  • Syntax error after editing: check braces and quotes, then run gofmt to identify formatting/parser errors.
  • go run main.go works but go run . does not: confirm go.mod is in the repository root and the command runs there.

results matching ""

    No results matching ""