Skip to content
/ go-backend-template Public template

This is a generic template for go backend using gin, gorm with postgres, docker for database, unit tests, makefiles, swagger docs, jwt implementation and so on.

License

Notifications You must be signed in to change notification settings

fuunshi/go-backend-template

Repository files navigation

Go Project

📌 Overview

This is a Go-based project that provides a structured backend implementation. It includes a Makefile for automation, linting, testing, and dependency management.


🚀 Getting Started

1️⃣ Prerequisites

Ensure you have the following installed:

  • Go (latest stable version)
  • Git
  • golangci-lint and goimports (installed automatically)
  • jq (for checking outdated dependencies)

2️⃣ Clone the Repository

git clone https://github.com/fuunshi/go-backend-template.git
cd go-backend-template

Change the project name(Optional)

bash script.sh

3️⃣ Setup the Project

Run the following command to install dependencies, format code, lint, and build the project:

make setup

This command: ✅ Installs required Go dependencies
✅ Installs golangci-lint and goimports (if missing)
✅ Formats the code (go fmt, goimports)
✅ Runs lint checks
✅ Checks for outdated dependencies
✅ Builds the project


4️⃣ Running the Project

🔹 Run in Development Mode

make dev

Runs the application directly without compiling it first.

🔹 Build and Run

make run

Compiles and runs the application.


🔨 Available Commands

Command Description
make setup Install dependencies, format, lint, and build the project.
make build Compile the application into bin/server.
make run Build and run the application.
make dev Run without building (useful for development).
make test Run all tests.
make lint Run static analysis using golangci-lint.
make lint-fix Auto-fix linting issues.
make fmt Format code using go fmt and goimports.
make tidy Ensure go.mod and go.sum are up to date.
make outdated Check for outdated dependencies.
make clean Remove compiled binaries.
make migrate Run database migrations using CLI commands.
make swagger Generate Swagger API documentation.
make mock Generate mocks for testing.
make seed Seed the database with initial data.
make install-tools Install required Go tools such as golangci-lint, goimports, and swag.

📦 Project Structure

.
├── docker-compose.yaml
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
├── lefthook.yml
├── Makefile
├── README.md
├── script.sh
└── src
    ├── api
    │   ├── auth
    │   │   ├── controller.go
    │   │   ├── repository.go
    │   │   ├── routes.go
    │   │   ├── schema.go
    │   │   └── service.go
    │   └── user
    │       ├── controller.go
    │       ├── dao.go
    │       ├── model.go
    │       ├── repository.go
    │       ├── routes.go
    │       ├── schema.go
    │       └── service.go
    ├── cmd
    │   ├── cli
    │   │   ├── main.go
    │   │   ├── migrate.go
    │   │   └── seed.go
    │   └── http
    │       ├── main.go
    │       └── server.go
    ├── common
    │   ├── model
    │   │   └── model.go
    │   └── schema
    │       └── schema.go
    ├── config
    │   ├── database.go
    │   └── env
    │       └── env.go
    ├── logger
    │   └── logger.go
    ├── middlewares
    │   ├── jwt_auth.go
    │   ├── rate_limit.go
    │   └── validation.go
    ├── service
    │   └── response.go
    ├── tests
    │   └── user_test.go
    └── utils
        └── jwt_util.go

🛠 Troubleshooting

"Command Not Found" Errors

If golangci-lint or goimports is missing, you can manually install them:

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install golang.org/x/tools/cmd/goimports@latest

Issues with Dependencies?

Run:

make tidy

This cleans up go.mod and go.sum.


🌟 Acknowledgments


🚀 Happy Coding!

About

This is a generic template for go backend using gin, gorm with postgres, docker for database, unit tests, makefiles, swagger docs, jwt implementation and so on.

Topics

Resources

License

Stars

Watchers

Forks