Skip to content
Proyectos

realworld-apps/golang-gin-realworld-example-app

Ver en GitHub

Acerca de

Aplicación de ejemplo del mundo real creada con Golang y Gin.

Aprendizaje y educación

Cómo se miden estas cifras

Crecimiento de estrellas medido

in 24h
+1
in 7 days
+3
in 15 days
+3
in 1 month
+13

Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.

README.md

Original

RealWorld Example App

CI Coverage Status License: MIT GoDoc

Golang/Gin codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

This codebase was created to demonstrate a fully fledged fullstack application built with Golang/Gin including CRUD operations, authentication, routing, pagination, and more.

Recent Updates

This project has been modernized with the following updates:

  • Go 1.21+: Updated from Go 1.15 to require Go 1.21 or higher
  • GORM v2: Migrated from deprecated jinzhu/gorm v1 to gorm.io/gorm v2
  • JWT v5: Updated from deprecated dgrijalva/jwt-go to golang-jwt/jwt/v5 (fixes CVE-2020-26160)
  • Validator v10: Updated validator tags and package to match gin v1.10.0
  • Latest Dependencies: All dependencies updated to their 2025 production-stable versions
  • RealWorld API Spec Compliance:
    • GET /profiles/:username now supports optional authentication (anonymous access allowed)
    • POST /users/login returns 401 Unauthorized on failure (previously 403)
    • GET /articles/feed registered as dedicated authenticated route
    • DELETE /articles/:slug and DELETE /articles/:slug/comments/:id return empty response body

Test Coverage

The project maintains high test coverage across all core packages:

Package Coverage
articles 93.4%
users 99.5%
common 85.7%
Total 90.0%

To generate a coverage report locally, run:

go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out

Dependencies (2025 Stable Versions)

Package Version Release Date Known Issues
gin-gonic/gin v1.10.0 2024-05 None; v1.11 has experimental HTTP/3 support
gorm.io/gorm v1.25.12 2024-08 None; v1.30+ has breaking changes
golang-jwt/jwt/v5 v5.2.1 2024-06 None; v5.3 only bumps Go version requirement
go-playground/validator/v10 v10.24.0 2024-12 None; v10.30+ requires Go 1.24
golang.org/x/crypto v0.32.0 2025-01 None; keep updated for security fixes
gorm.io/driver/sqlite v1.5.7 2024-09 None; requires cgo; use glebarez/sqlite for pure Go
gosimple/slug v1.15.0 2024-12 None
stretchr/testify v1.10.0 2024-10 None; v2 still in development

Directory structure

.
├── gorm.db
├── hello.go
├── common
│   ├── utils.go        //small tools function
│   └── database.go     //DB connect manager
├── users
|   ├── models.go       //data models define & DB operation
|   ├── serializers.go  //response computing & format
|   ├── routers.go      //business logic & router binding
|   ├── middlewares.go  //put the before & after logic of handle request
|   └── validators.go   //form/json checker
├── ...
...

Getting started

Install Golang

Make sure you have Go 1.21 or higher installed.

https://golang.org/doc/install

Environment Config

Environment variables can be set directly in your shell or via a .env file (requires a tool like source or direnv).

Available environment variables:

PORT=8080                     # Server port (default: 8080)
GIN_MODE=debug               # Gin mode: debug or release
DB_PATH=./data/gorm.db       # SQLite database path (default: ./data/gorm.db)
TEST_DB_PATH=./data/test.db  # Optional: SQLite database path used for tests

Example usage:

# Option 1: Set environment variables directly
export PORT=3000
export DB_PATH=./data/myapp.db
go run hello.go

# Option 2: Inline with command
PORT=3000 go run hello.go

See .env.example for a complete template.

Install Dependencies

From the project root, run:

go build ./...
go test ./...
go mod tidy

Run the Server

# Using default port 8080
go run hello.go

# Using custom port
PORT=3000 go run hello.go

Testing

From the project root, run:

go test ./...

API spec tests (Hurl & Bruno)

This backend passes the full RealWorld API spec test suite, available in two flavors (Hurl is the source of truth; the Bruno collection is generated from it). The run-spec-tests.sh script downloads the specs at a pinned commit (sha256-verified tarball, go.sum-style), builds the app, starts a temporary server and runs the chosen suite against it:

# Hurl (requires https://hurl.dev)
bash ./scripts/run-spec-tests.sh hurl

# Bruno (requires node/npx or bun)
bash ./scripts/run-spec-tests.sh bruno

Both suites run in CI (hurl-api-test and bruno-api-test jobs) and are the authoritative compliance check. (They replace the retired upstream Postman/Newman collection, which asserted a strict subset of the same behavior.)

To bump the spec version, update SPECS_REF_DEFAULT (upstream commit) and SPECS_SHA256_DEFAULT (tarball checksum) in scripts/run-spec-tests.sh. To try a different version ad hoc: SPECS_REF=main bash ./scripts/run-spec-tests.sh hurl (checksum verification is skipped unless you also pass SPECS_SHA256). A weekly spec-drift workflow runs the suite against upstream main so we notice when the spec moves without blocking PRs. or

go test ./... -cover

or

go test -v ./... -cover

depending on whether you want to see test coverage and how verbose the output you want.

Todo

  • More elegance config
  • ProtoBuf support
  • Code structure optimize (I think some place can use interface)
  • Continuous integration (done)

Test Coverage

Current test coverage (2026):

  • Total: 89.2%
  • articles: 92.1%
  • users: 99.5%
  • common: 85.7%

Run coverage report:

go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out

Explorar proyectos relacionados

Cargando…