golang 每日一庫之 DDD 框架 freedo
Freedom:自由不是亂來,而是架構有道
你是不是寫 Go 寫得挺開心的,直到有一天產品說:
“這個業務流程以後會變、會拆、可能還會上天。”
於是你開始發愁:
-
怎麼寫纔不會三個月後改到哭?
-
怎麼解耦、怎麼擴展、怎麼不踩坑?
這時候,Freedom 躍馬而來,大喊一句:
“用我!讓你寫得自由、改得瀟灑、跑得像風!”
什麼是 Freedom
Freedom 是一個 基於 “領域驅動設計(DDD)+ 清晰分層架構” 的 Go Web 框架。它不搞花裏胡哨的魔法,講究的就是:
結構清晰,職責分明,穩定如老狗。
它有啥特別的
大餅來一張
請求 -> Controller (Adapter)
-> Service Interface
-> Domain Service
-> Model
-> Infra(存儲、消息、外部API)
換句話說:
-
Controller
負責 “人類語言” 轉“程序語言”
-
Service Interface
像客服,幫你轉接不同業務員
-
Domain
是核心大腦,掌控業務邏輯
-
Infra
是工具人,存數據庫、發消息、調外部接口
再畫兩個餅
場景一:複雜業務系統(比如電商)
你要做用戶下單 ➜ 優惠券校驗 ➜ 積分計算 ➜ 庫存扣減 ➜ 發消息 ➜ 多語言 ➜ 可擴展 ➜ 不出事?
Freedom:請上車,我把這流程拆成優雅的服務、層、接口,每一層都單純得像小學生,後期隨便加功能都不怕亂。
場景二:遊戲服務端 / 金融系統
你需要:
-
高穩定性(掛了不能連鍋)
-
業務邏輯多變(策劃說明天改規則)
-
可擴展性強(未來可能接 AI)
Freedom 提供了類似 “編寫遊戲引擎” 的開發體驗,每個組件都可替換、抽象,每個依賴都可注入。
它不適合誰
-
想 30 分鐘糊一個簡單 API 服務上線的開發者(Go 的 net/http 就夠了)
-
不想搞結構、只想全寫在 handler 的朋友(Freedom 會把你拽到 “架構正道”)
喫飽了舉個例子
目標功能
我們實現一個簡單的 “用戶管理系統”,支持:
-
獲取用戶信息(GET)
-
創建用戶(POST)
項目結構概覽(符合 Freedom 的分層哲學)
user-app/
├── adapter/ # 控制器層,對外暴露接口(Controller)
│ └── user.go
├── application/ # 接口層,定義服務接口
│ └── user_service.go
├── domain/ # 領域層,業務邏輯核心
│ └── user_entity.go
│ └── user_domain_service.go
├── infrastructure/ # 基礎設施層,如數據庫
│ └── user_repository.go
├── model/ # 實體 & 數據結構
│ └── user.go
├── vo/ # 請求和返回的數據結構
│ └── user_vo.go
├── main.go # 啓動文件
└── config/
└── config.toml
示例核心代碼(刪繁就簡)
控制器層(Controller)
// adapter/user.go
type UserController struct {
UserService application.UserService
}
func (u *UserController) GetUser(ctx freedom.Context) (vo.UserVO, error) {
id := ctx.QueryInt("id")
return u.UserService.GetUser(ctx, id)
}
func (u *UserController) CreateUser(ctx freedom.Context) (vo.UserVO, error) {
var req vo.UserCreateRequest
err := ctx.Bind(&req)
if err != nil {
return vo.UserVO{}, err
}
return u.UserService.CreateUser(ctx, req)
}
應用服務接口層(interface)
// application/user_service.go
type UserService interface {
GetUser(ctx freedom.Context, id int) (vo.UserVO, error)
CreateUser(ctx freedom.Context, req vo.UserCreateRequest) (vo.UserVO, error)
}
領域邏輯層(Domain)
// domain/user_domain_service.go
type UserDomainService struct {
UserRepo *infrastructure.UserRepository
}
func (u *UserDomainService) GetUserByID(id int) (*model.User, error) {
return u.UserRepo.FindByID(id)
}
func (u *UserDomainService) CreateUser(name string) (*model.User, error) {
user := &model.User{Name: name, CreatedAt: time.Now()}
err := u.UserRepo.Save(user)
return user, err
}
基礎設施層(Infra)
// infrastructure/user_repository.go
type UserRepository struct {
freedom.Repository
}
func (r *UserRepository) FindByID(id int) (*model.User, error) {
var user model.User
err := r.FindByIDTo(id, &user)
return &user, err
}
func (r *UserRepository) Save(user *model.User) error {
return r.Save(user)
}
模型層 & VO
// model/user.go
type User struct {
ID int
Name string
CreatedAt time.Time
}
// vo/user_vo.go
type UserCreateRequest struct {
Name string `json:"name"`
}
type UserVO struct {
ID int `json:"id"`
Name string `json:"name"`
}
啓動(main.go)
func main() {
app := freedom.NewApplication()
app.InstallController(func() freedom.Controller {
return &adapter.UserController{}
})
app.Run()
}
請求演示
-
GET
/user?id=1
返回:{"id": 1, "name": "張三"} -
POST
/userwith body{"name": "李四"}
返回:{"id": 2, "name": "李四"}
總結一下
這套架構結構清晰、解耦徹底、邏輯分明,非常適合中大型系統的擴展與演化。
-
想換數據庫?改 Infra;
-
想換業務規則?改 Domain;
-
想測試業務?Mock Application;
-
想暴露不同接口?加 Controller;
就像搭積木一樣,每一塊都可以獨立維護,改某一層不容易炸整棟樓。
標題:golang 每日一庫之 DDD 框架 freedom
作者:mooncakeee
地址:http://blog.dd95828.com/articles/2025/06/05/1749086398473.html
聯繫:scotttu@163.com
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/Veb4niRPlz-L9dFLN1ondQ