This document provides instructions for AI coding agents working on this repository.
This is a Golang/Gin implementation of the RealWorld example application. It demonstrates a fully fledged fullstack application including CRUD operations, authentication, routing, pagination, and more.
- Go: 1.21+ required
- Web Framework: Gin v1.10+
- ORM: GORM v2 (gorm.io/gorm)
- Database: SQLite (for development/testing)
- Authentication: JWT using golang-jwt/jwt/v5
- Validation: go-playground/validator/v10
.
├── hello.go # Main entry point
├── common/ # Shared utilities
│ ├── database.go # Database connection manager
│ ├── utils.go # Helper functions (JWT, validation, etc.)
│ └── unit_test.go # Common package tests
├── users/ # User module
│ ├── models.go # User data models & DB operations
│ ├── serializers.go # Response formatting
│ ├── routers.go # Route handlers
│ ├── middlewares.go # Auth middleware
│ ├── validators.go # Input validation
│ └── unit_test.go # User package tests
├── articles/ # Articles module
│ ├── models.go # Article data models & DB operations
│ ├── serializers.go # Response formatting
│ ├── routers.go # Route handlers
│ ├── validators.go # Input validation
│ └── unit_test.go # Article package tests
└── scripts/ # Build/test scripts
# Install dependencies
go mod download
# Build
go build ./...
# Run tests
go test ./...
# Run tests with coverage
go test -coverprofile=coverage.out ./...
# Format code
go fmt ./...
# Run linter
golangci-lint run
# Start the server
go run hello.go- Error Handling: Always handle errors explicitly. Do not ignore errors with
_. - GORM v2 Patterns:
- Use
Preload()instead ofRelated()for eager loading - Use
Association().Find()for many-to-many relationships - Use
Updates()instead ofUpdate()for struct/map updates - Use pointers with
Delete():Delete(&Model{}) - Count returns
int64, handle overflow when converting touint
- Use
- Validation Tags: Use
requiredinstead of deprecatedexiststag - JWT: Use
jwt.NewWithClaims()for token creation
- Tests are in
*_test.gofiles alongside source code - Use
common.TestDBInit()andcommon.TestDBFree()for test database setup/teardown - Run
go test ./...before committing changes
The API follows the RealWorld API Spec:
POST /api/users- RegisterPOST /api/users/login- LoginGET /api/user- Get current userPUT /api/user- Update userGET /api/profiles/:username- Get profilePOST /api/profiles/:username/follow- Follow userDELETE /api/profiles/:username/follow- Unfollow userGET /api/articles- List articlesGET /api/articles/feed- Feed articlesGET /api/articles/:slug- Get articlePOST /api/articles- Create articlePUT /api/articles/:slug- Update articleDELETE /api/articles/:slug- Delete articlePOST /api/articles/:slug/comments- Add commentGET /api/articles/:slug/comments- Get commentsDELETE /api/articles/:slug/comments/:id- Delete commentPOST /api/articles/:slug/favorite- Favorite articleDELETE /api/articles/:slug/favorite- Unfavorite articleGET /api/tags- Get tags
- JWT secret is defined in
common/utils.go- do not expose in production - Always validate user input using validator tags
- Use parameterized queries (GORM handles this automatically)