Getting Started

View saved

Goal

This snapshot advances Cedarline Post by teaching you to create a minimal Go HTTP app on port 8081.

Every numbered folder is a complete app. Run this stage independently, then compare it with the previous folder.

Prerequisites

Use Go 1.22+, a terminal, and a code editor. You should recognize packages, functions, and HTTP verbs.

Port 8081 must be free. Copy .env.example before starting. Stages from the data layer use a per-snapshot SQLite file (no Docker required).

  • Go 1.22 or newer
  • A code editor and terminal
  • Optional: Docker only if you prefer another database later

Concepts

The standard library net/http serves JSON from a handler. Chi will arrive next for clearer routing.

Keep handlers thin: routes accept input, SQL and helpers enforce rules, and html/template escapes output by default.

Walkthrough

Register a root handler, return a JSON message, and listen on port 8081.

Read the example, then open the matching snapshot. The repository includes the surrounding setup and error handling.

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprint(w, `{"message":"Cedarline Post is live"}`)
})

Run and verify

Enter 01-Getting-Started, copy .env.example, run go mod tidy, and start the server on port 8081.

Open http://127.0.0.1:8081. Watch the terminal for validation and database errors.

git clone https://github.com/michaeldunga1/fcc-go-blog.git
cd fcc-go-blog/01-Getting-Started
cp .env.example .env
go mod tidy
go run .

Troubleshooting

If the browser cannot connect, confirm the process is still running. Address already in use means another process owns 8081.

Missing module errors usually mean go mod tidy was not run in this snapshot. For SQLite failures, delete a corrupt *.db file and restart so migrate/seed can recreate it.

  • Read the first error first
  • Restart after environment changes
  • Never commit .env or *.db

Try this

Add a /health route that returns JSON ok: true.

Test a happy path and one invalid or unauthorized request.

  • Make one small change
  • Test it in the browser
  • Compare with the next snapshot only after it works

Next: Routing

Comments

One comment per signed-in account. Comments are saved with this page’s URL.