Fullstack Golang example — build a Go web app with the Gin framework, PostgreSQL, REST JSON APIs, and server-rendered HTML. A practical college project for learning Go backend development.
Lab 3 in the Go learning path · Audience: intermediate beginners · Time: ~2–3 hours · Level: intermediate
A minimal book catalog application:
- Frontend: Go
html/templatepage (templates/index.html) - Backend: Gin HTTP router
- Database: PostgreSQL via
lib/pq - API: list and create books (
GET/POST /api/books)
Search keywords: Golang Gin PostgreSQL tutorial, fullstack Go example, Go REST API CRUD books, Gin web framework beginner project, Go college project with database.
- REST endpoints for books (list + create)
- PostgreSQL persistence
- HTML home page served by Gin
- Environment-based
DATABASE_URL - SQL helpers (
book.sql,permissions.sql)
| Tool | Version |
|---|---|
| Go | 1.21+ (module may list a newer toolchain — install latest stable Go) |
| PostgreSQL | 14+ recommended |
| psql / admin access | to create DB and user |
Complete Labs 0–2 first if you are new to Go.
git clone https://github.com/saurabhahuja71/fullstack-go-bookapp-example.git
cd fullstack-go-bookapp-example
go mod downloadsudo -u postgres psqlCREATE DATABASE bookdb;
CREATE USER bookuser WITH ENCRYPTED PASSWORD 'bookpass';
GRANT ALL PRIVILEGES ON DATABASE bookdb TO bookuser;
\c bookdb
GRANT USAGE, CREATE ON SCHEMA public TO bookuser;Apply schema (if using provided SQL files):
psql "postgres://bookuser:bookpass@localhost:5432/bookdb?sslmode=disable" -f book.sql
psql "postgres://bookuser:bookpass@localhost:5432/bookdb?sslmode=disable" -f permissions.sqlIf
book.sqlexpects abookstable, ensure it matches fieldsid,title,authorused inmain.go.
export DATABASE_URL="postgres://bookuser:bookpass@localhost:5432/bookdb?sslmode=disable"
go run .- Web UI: http://localhost:8080/
- API list: http://localhost:8080/api/books
Gin listens on 8080 by default (or PORT if set in your environment).
curl http://localhost:8080/api/books
curl -X POST http://localhost:8080/api/books \
-H "Content-Type: application/json" \
-d '{"title":"The Go Programming Language","author":"Donovan & Kernighan"}'fullstack-go-bookapp-example/
├── main.go # Gin routes + handlers
├── database.go # DB init from DATABASE_URL
├── book.sql # schema
├── permissions.sql # grants
├── templates/index.html # simple UI
├── go.mod
└── go.sum
Browser → Gin router
├─ GET / → HTML template
├─ GET /api/books → JSON list from Postgres
└─ POST /api/books → INSERT then JSON message
- Add GET /api/books/:id and DELETE /api/books/:id.
- Validate empty title/author and return HTTP 400.
- Replace string concatenation risks (already using
$1,$2— good) and add logging. - Docker Compose:
app+postgresservices. - Stretch: switch HTML to a small React/Vite UI calling the same API (see grpc-golang-todo for a fuller stack).
| # | Lab | Focus |
|---|---|---|
| 2 | gotraining-labs | Language depth |
| 3 (this) | Book app | Gin + SQL web service |
| 4 | grpc-golang-todo | gRPC + gateway + React + Postgres |
Hub: learning-path
Gin vs net/http?
net/http is the standard library. Gin adds routing, middleware, and JSON helpers — common in industry tutorials.
Can I use SQLite instead of Postgres?
Yes, with a different driver — but this lab intentionally teaches Postgres, widely used in production.
Is this production ready?
No. Missing auth, migrations tooling, and tests — perfect as a learning skeleton.
golang gin postgresql fullstack rest-api go-web tutorial book-app crud college-project backend html-templates
Educational sample for learning Go web development.