Skip to content

slowhigh/goclean

Repository files navigation

goclean

Build Status Go Report Card codebeat badge

You can start developing right away in Go (Golang) with a streamlined Clean Architecture setup that eliminates unnecessary elements.

Contents

Project structure

  • Clean Architecture
    • Layer Mapping
      • /infra ------------------------------------------ "Frameworks and Drivers" Layer
      • /controller ------------------------------------ "Interface Adapters" Layer
      • /usecase ---------------------------------------- "Use Cases" Layer
      • /entity ----------------------------------------- "Entities" Layer
    • Crossing boundaries
      • google/wire: Compile-time Dependency Injection for Go (DI/IoC)
  • Official standard layout (feat. Go dev team)
  • De facto standard layout (feat. golang-standards)
├── cmd --------------------------------------------- De facto standard layout
│  └── server
│     ├── main.go
│     ├── wire_gen.go ------------------------------- Code generated by "Google/Wire".
│     └── wire.go ----------------------------------- Core code of "Google/Wire"
│
├── infra ------------------------------------------- "Frameworks and Drivers" Layer
│  ├── infra.go
│  ├── config
│  │  ├── config.go
│  │  └── config.json
│  ├── database
│  │  ├── database.go
│  │  └── repository
│  │     └── memo_repo.go
│  └── router
│     ├── router.go
│     ├── handler
│     │  └── memo_handler.go
│     └── middleware
│        └── middleware.go
│
├── internal ---------------------------------------- Official standard layout
│  ├── controller ----------------------------------- "Interface Adapters" Layer
│  │  ├── controller.go
│  │  └── rest
│  │     ├── dto
│  │     │  ├── memo_dto
│  │     │  │  ├── memo_create_dto.go
│  │     │  │  ├── memo_delete_dto.go
│  │     │  │  ├── memo_find_all_dto.go
│  │     │  │  ├── memo_find_one_dto.go
│  │     │  │  └── memo_update_dto.go
│  │     │  └── pagination_dto.go
│  │     └── memo_controller.go
│  ├── usecase -------------------------------------- "Use Cases" Layer
│  │  ├── usecase.go
│  │  └── memo
│  │     └── memo_ucase.go
│  └── entity --------------------------------------- "Entities" Layer
│     └── memo.go
│
└── third_party ------------------------------------- De facto standard layout
   └── docs
      ├── docs.go
      ├── swagger.json
      └── swagger.yaml

Getting started

  1. Setup PostgreSQL dependency

    docker run -d --name goclean --env=POSTGRES_USER=goclean --env=POSTGRES_PASSWORD=goclean1! --env=POSTGRES_DB=goclean --env=TIMEZONE=Asia/Seoul -p 5432:5432 postgres
  2. Download modules

    go mod download
  3. Run the server

    go run ./cmd/server .
  4. Go to http://localhost:5000/v1/docs/index.html, you to see your Swagger UI for API test

Make Command

  • Prerequisites

    # "google/wire" module
    go install github.com/google/wire/cmd/wire@latest
    
    # "swaggo/swag" module
    go install github.com/swaggo/swag/cmd/swag@latest
    
    # "golang.org/x/tools" module
    go install golang.org/x/tools/cmd/goimports@latest
  • Command

    • make run run the server
    • make di generate injectors from wire.go
    • make format formats code
    • make docs generate swagger file

References