Skip to content

About

Fullstack Go tutorial: Gin + PostgreSQL book app with REST API and HTML templates

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Fullstack Go Tutorial: Book App with Gin, PostgreSQL & HTML Templates

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

What is this project?

A minimal book catalog application:

  • Frontend: Go html/template page (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.

Features

  • REST endpoints for books (list + create)
  • PostgreSQL persistence
  • HTML home page served by Gin
  • Environment-based DATABASE_URL
  • SQL helpers (book.sql, permissions.sql)

Prerequisites

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.

Quick start

1. Clone and dependencies

git clone https://github.com/saurabhahuja71/fullstack-go-bookapp-example.git
cd fullstack-go-bookapp-example
go mod download

2. Create PostgreSQL database

sudo -u postgres psql
CREATE 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.sql

If book.sql expects a books table, ensure it matches fields id, title, author used in main.go.

3. Run the app

export DATABASE_URL="postgres://bookuser:bookpass@localhost:5432/bookdb?sslmode=disable"
go run .

Gin listens on 8080 by default (or PORT if set in your environment).

4. Try the API

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"}'

Project structure

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

Architecture (student view)

Browser → Gin router
            ├─ GET  /           → HTML template
            ├─ GET  /api/books  → JSON list from Postgres
            └─ POST /api/books  → INSERT then JSON message

Lab exercises

  1. Add GET /api/books/:id and DELETE /api/books/:id.
  2. Validate empty title/author and return HTTP 400.
  3. Replace string concatenation risks (already using $1,$2 — good) and add logging.
  4. Docker Compose: app + postgres services.
  5. Stretch: switch HTML to a small React/Vite UI calling the same API (see grpc-golang-todo for a fuller stack).

Learning path

# 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

FAQ — Fullstack Go for beginners

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.

Topics / SEO tags

golang gin postgresql fullstack rest-api go-web tutorial book-app crud college-project backend html-templates

Author

Saurabh Ahuja · learning-path

License

Educational sample for learning Go web development.

About

Fullstack Go tutorial: Gin + PostgreSQL book app with REST API and HTML templates

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages