Scaffold Go REST API with Gin or Chi router
✓Works with OpenClaudeYou are a Go backend developer. The user wants to scaffold a complete REST API project using either Gin or Chi router with proper structure, middleware, and example endpoints.
What to check first
- Run
go versionto confirm Go 1.19+ is installed - Verify
$GOPATH/binis in your$PATHfor any CLI tools - Check if you're starting in an empty directory or existing Go module
Steps
- Initialize Go module with
go mod init github.com/yourusername/your-api-name - Install Gin router:
go get -u github.com/gin-gonic/gin(or Chi:go get github.com/go-chi/chi/v5) - Create project structure:
mkdir -p internal/handlers internal/models internal/middleware cmd/api - Create
internal/models/user.gowith struct definitions for your data types - Create
internal/handlers/user.goto define route handler functions with proper signatures - Create
internal/middleware/logging.gofor custom middleware (CORS, logging, error handling) - Create
cmd/api/main.goas entry point that initializes router and starts server on:8080 - Run
go mod tidyto clean up dependencies, thengo run ./cmd/apito test the server
Code
// cmd/api/main.go
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"your-module/internal/handlers"
"your-module/internal/middleware"
)
func main() {
router := gin.Default()
// Apply middleware
router.Use(middleware.LoggingMiddleware())
router.Use(middleware.ErrorHandlingMiddleware())
// Health check endpoint
router.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
// User routes
api := router.Group("/api/v1")
{
api.GET("/users", handlers.GetUsers)
api.POST("/users", handlers.CreateUser)
api.GET("/users/:id", handlers.GetUserByID)
api.PUT("/users/:id", handlers.UpdateUser)
api.DELETE("/users/:id", handlers.DeleteUser)
}
log.Println("Server running on :8080")
if err := router.Run(":8080"); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
// internal/models/user.go
package models
type User struct {
ID int `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required,email"`
Age int `json:"age"`
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Go Skills
Other Claude Code skills in the same category — free to download.
Go Testing
Set up Go testing with table-driven tests and mocks
Go Modules
Manage Go modules and dependency versioning
Go Concurrency
Implement goroutines, channels, and concurrency patterns
Go Docker
Create optimized multi-stage Docker build for Go apps
Go gRPC
Set up gRPC server and client in Go
Go Middleware
Create HTTP middleware chain in Go
Go CLI
Build CLI applications with Cobra in Go
Want a Go skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.