序
本文主要研究一下golang的clean architecture項(xiàng)目結(jié)構(gòu)
clean architecure

clean architecture定義了四層結(jié)構(gòu),最內(nèi)層是entities(
enterprise business rules),再往外是use cases(application business rules),接著是interface adapters(比如controller、presenters、gateways),最外層是frameworks & drivers(比如web、ui、db、devices、external interfaces)
app
go-cleanarchitecture-sample/src/app
├── domain
│ └── user.go
├── glide.lock
├── glide.yaml
├── infrastructure
│ ├── router.go
│ └── sqlhandler.go
├── interfaces
│ ├── controllers
│ │ ├── context.go
│ │ ├── error.go
│ │ └── user_controller.go
│ └── database
│ ├── sqlhandler.go
│ └── user_repository.go
├── server.go
└── usecase
├── user_interactor.go
└── user_repository.go
domain層定義了領(lǐng)域模型及相關(guān)領(lǐng)域方法;usecase層定義了業(yè)務(wù)用例方法及相關(guān)接口,然后進(jìn)行編排實(shí)現(xiàn);infrastructure層是對(duì)一些基礎(chǔ)服務(wù)/類庫(kù)的管理;interfaces層這里對(duì)輸入輸出進(jìn)行適配實(shí)現(xiàn)use case層定義的接口
小結(jié)
clean architecture主要是分了4層結(jié)構(gòu),domain層,有的會(huì)把repository接口放在這一層,然后domain service會(huì)調(diào)用repository;use case層對(duì)應(yīng)ddd的application層,主要是業(yè)務(wù)編排,有的也把repository接口放在這一層;interfaces adapters層會(huì)對(duì)輸入和輸出進(jìn)行適配,實(shí)現(xiàn)use case定義的方法,類似ddd的interfaces層;infrastructure層主要是對(duì)基礎(chǔ)服務(wù)/類庫(kù)的管理,有些工程把對(duì)repository的實(shí)現(xiàn)也放這里了,貌似不太妥當(dāng)。