Go Fundamentals & REST APIs
Overview
Go (Golang) is a statically typed, compiled language designed for high‑performance backend systems. This tutorial covers project setup, building HTTP servers with the standard library, implementing REST APIs, and integrating with databases using GORM.
1. Introduction to Go
Key features of Go:
- Simplicity: Minimal syntax, easy to learn and read.
- Concurrency: Goroutines and channels for concurrent programming.
- Fast compilation: Compiles to a single binary.
- Garbage collection: Automatic memory management.
- Standard library: Rich standard library with excellent HTTP support.
2. Setting Up a Go Project
Installation
Download Go from golang.org.
Project Setup
Using Gin (popular web framework) or the standard library:
3. HTTP Servers with Standard Library
Go's standard library has excellent HTTP support.
4. Routing & Handlers
Gin provides expressive routing with parameter extraction.
5. Database Access with GORM
GORM is the most popular ORM for Go.
Quiz
Question 1
Which command initialises a Go module?
- go init
- go mod init
- go create
- go new
Show answer
Question 2
Which package is the most popular web framework in Go?
- http
- gin
- echo
- fiber
Show answer
Question 3
Which ORM is most commonly used in Go?
- GORM
- Sequelize
- Hibernate
- TypeORM
Show answer
Exercises
Exercise 1
Create a User struct with ID, Name, Email. Write a handler that
returns a list of users.
Sample answer
Exercise 2
Add a POST /api/users endpoint that accepts JSON and returns
the created user.
Sample answer
Homework
Homework 1
Build a complete CRUD API for a Todo resource with fields: ID,
Title, Completed. Use GORM and Gin.
Sample outline
- Model: Todo (ID, Title, Completed)
- Handlers: GET /api/todos, GET /api/todos/:id, POST /api/todos, PUT /api/todos/:id, DELETE /api/todos/:id
- Database: SQLite or PostgreSQL with GORM
- Main: Setup routes, connect to DB, run server
Mini‑Project
Task Manager API
Build a Task Manager API with Go and Gin:
- Task: ID, Title, Description, Status (pending/in progress/done), CreatedAt
- CRUD endpoints
- Filter by status: GET /api/tasks?status=pending
- Use GORM with SQLite
- Add validation for required fields
Sample implementation outline
- Model: Task struct with gorm tags
- Repository: Functions for CRUD operations
- Handlers: Gin handlers with error handling
- Query params: c.Query("status") for filtering
- Validation: Check required fields before saving
Test using curl or Postman.
Tutorial Summary
You learned the fundamentals of Go backend development: project setup, building HTTP servers with the standard library and Gin, implementing REST APIs with routing and handlers, and integrating with databases using GORM. You built a solid foundation for high‑performance backend development in Go.
Key takeaway: Go's simplicity, performance, and excellent concurrency model make it a compelling choice for modern backend systems. Its standard library provides everything you need to build production‑ready APIs.