当前位置: 首页 > news >正文

go: Fail-Fast Pattern

项目结构:

/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:25 # User : geovindu # Product : GoLand # Project : godesginpattern # File : fail_fast.go */ package exceptions type ResourceCheckFailedError struct { Msg string } func (e *ResourceCheckFailedError) Error() string { return e.Msg } func NewFailFastErr(msg string) error { return &ResourceCheckFailedError{Msg: msg} } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:26 # User : geovindu # Product : GoLand # Project : godesginpattern # File : inventory_repo.go */ package repository type InventoryRepository struct { stockMap map[string]int } func NewInventoryRepository() *InventoryRepository { repo := &InventoryRepository{ stockMap: make(map[string]int), } // 初始化测试库存 repo.stockMap["DIA001"] = 2 repo.stockMap["DIA002"] = 0 return repo } func (r *InventoryRepository) GetStock(productId string) int { val, ok := r.stockMap[productId] if !ok { return -1 } return val } func (r *InventoryRepository) DeductStock(productId string, qty int) { r.stockMap[productId] -= qty } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:27 # User : geovindu # Product : GoLand # Project : godesginpattern # File : external_services_repo.go */ package repository type ExternalServices struct { InventoryService bool PaymentGateway bool QualityInspection bool InvoiceService bool } func NewExternalServices() *ExternalServices { return &ExternalServices{ InventoryService: true, PaymentGateway: true, QualityInspection: true, InvoiceService: true, } } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:27 # User : geovindu # Product : GoLand # Project : godesginpattern # File : validation_service.go */ package service import ( "godesginpattern/failfast/exceptions" "godesginpattern/failfast/repository" ) type ValidationService struct { invRepo *repository.InventoryRepository extSvc *repository.ExternalServices } func NewValidationService(inv *repository.InventoryRepository, ext *repository.ExternalServices) *ValidationService { return &ValidationService{ invRepo: inv, extSvc: ext, } } func (v *ValidationService) FailFastCheck(customerId, productId string, quantity int) error { // 1. 顾客校验 if len(customerId) < 3 { return exceptions.NewFailFastErr("顾客信息无效,立即失败") } // 2. 商品是否存在 stock := v.invRepo.GetStock(productId) if stock == -1 { return exceptions.NewFailFastErr("商品不存在,立即失败") } // 3. 库存校验 if stock < quantity { return exceptions.NewFailFastErr("库存不足,立即失败") } // 4. 外部服务校验 if !v.extSvc.InventoryService { return exceptions.NewFailFastErr("库存服务不可用,立即失败") } if !v.extSvc.PaymentGateway { return exceptions.NewFailFastErr("支付网关不可用,立即失败") } if !v.extSvc.QualityInspection { return exceptions.NewFailFastErr("质检服务不可用,立即失败") } if !v.extSvc.InvoiceService { return exceptions.NewFailFastErr("发票服务不可用,立即失败") } return nil } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:28 # User : geovindu # Product : GoLand # Project : godesginpattern # File : order_service.go */ package service import ( "godesginpattern/failfast/repository" //"godesginpattern/failfast/exceptions" "strconv" "time" ) type OrderService struct { valSvc *ValidationService invRepo *repository.InventoryRepository extSvc *repository.ExternalServices } func NewOrderService(val *ValidationService, inv *repository.InventoryRepository, ext *repository.ExternalServices) *OrderService { return &OrderService{ valSvc: val, invRepo: inv, extSvc: ext, } } func (o *OrderService) CreateOrder(customerId, productId string, quantity int) (map[string]interface{}, error) { err := o.valSvc.FailFastCheck(customerId, productId, quantity) if err != nil { return nil, err } // 校验通过执行业务 o.invRepo.DeductStock(productId, quantity) orderId := "ORD" + strconv.FormatInt(time.Now().Unix(), 10) res := map[string]interface{}{ "order_id": orderId, "product_id": productId, "quantity": quantity, "remaining_stock": o.invRepo.GetStock(productId), "status": "success", } return res, nil } /* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:31 # User : geovindu # Product : GoLand # Project : godesginpattern # File : order_controller.go */ package api import ( "fmt" "godesginpattern/failfast/exceptions" "godesginpattern/failfast/service" ) type OrderController struct { orderSvc *service.OrderService } func NewOrderController(svc *service.OrderService) *OrderController { return &OrderController{ orderSvc: svc, } } func (c *OrderController) CreateJewelryOrder(customerId, productId string, quantity int) string { res, err := c.orderSvc.CreateOrder(customerId, productId, quantity) if err != nil { if e, ok := err.(*exceptions.ResourceCheckFailedError); ok { return "\n❌ 快速失败: " + e.Msg } return "\n❌ 业务异常: " + err.Error() } return "\n✅ 订单创建成功!详情:" + fmt.Sprintf("%v", res) }

调用:

/* # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:failfast # Author : geovindu,Geovin Du 涂聚文. # IDE : goLang 2024.3.6 go 26.2 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 21:32 # User : geovindu # Product : GoLand # Project : godesginpattern # File : failfastbll.go */ package bll import ( "fmt" "godesginpattern/failfast/api" "godesginpattern/failfast/repository" "godesginpattern/failfast/service" ) func FailfastMain() { // 初始化依赖(和Python代码一一对应) inventoryRepo := repository.NewInventoryRepository() extServices := repository.NewExternalServices() validationService := service.NewValidationService(inventoryRepo, extServices) orderService := service.NewOrderService(validationService, inventoryRepo, extServices) controller := api.NewOrderController(orderService) // ====================== // 测试场景 完全对齐Python输出 // ====================== fmt.Println("==== 场景1:正常下单 ===") fmt.Println(controller.CreateJewelryOrder("C1001", "DIA001", 1)) fmt.Println("\n==== 场景2:库存不足 ===") fmt.Println(controller.CreateJewelryOrder("C1001", "DIA002", 1)) fmt.Println("\n==== 场景3:顾客无效 ===") fmt.Println(controller.CreateJewelryOrder("C", "DIA001", 1)) fmt.Println("\n==== 场景4:支付服务挂了 ===") extServices.PaymentGateway = false fmt.Println(controller.CreateJewelryOrder("C1001", "DIA001", 1)) }

输出:

http://www.jsqmd.com/news/1099541/

相关文章:

  • ​​​​​​​旧尺子量新人:当求职者的“新技能”遇上面试官的“旧思维”
  • 万能遥控器app,各类家具都可用,推荐安装!
  • 【MES】自研MES采集设备数据的坑
  • 【2026最新】Adobe InDesign:Id2026专业排版神器
  • 基于STM32单片机的颜色识别 TCS3200 RGB 检测系统2(设计源文件+万字报告+讲解)(支持资料、图片参考_降重降ai)
  • Python 基础入门:列表、字典、函数与类,一篇搞定核心概念本文将从零开始,带你掌握 Python 最核心的四个概念:列表、字典、函数和类。
  • emanjusaka——彼岸花开可奈何
  • 2026主流EPC项目协同平台横向选型与避坑评测
  • Manus小程序邀请码获取渠道+教程,附手机版+PC官网
  • NET 安装 Aspose.Email for Python - Outlook SDK 安装
  • 基于STM32单片机火灾报警系统 智能楼宇 烟雾温度火焰防盗无线2(设计源文件+万字报告+讲解)(支持资料、图片参考_降重降ai)
  • Qt阅读器-缩略图
  • Go语言代码覆盖率实现一、什么是代码覆盖率
  • LLM喂文件神器-讲讲开源文件转换工具 file2md
  • 企业DLP选型指南:从入门到决策,一篇讲透
  • 10 种 RAG 模式
  • 你的 Agent 架构选错了:越复杂的 Agent 系统,越可能走向失败
  • 工业互联网组建与维护核心流程与实战要点
  • 什么是 Vaadin?
  • Fan Control完整教程:5个实用技巧优化电脑散热性能
  • 鸿蒙系统进一步学习(三):ArkUI的差分渲染
  • 3D CAD SDK 安装
  • Spring AI + RAG
  • 大模型服务弹性伸缩:从 GPU 利用率到 K8s HPA 的全链路实战
  • 告别Keil律师函!手把手教你用VSCode+GNU Arm+STM32CubeMX搭建免费单片机开发环境(Windows版)
  • 从零到一:基于Dify的AI应用开发全流程实践指南
  • 气泡特效的核心在于BubbleEffect类,它继承自Manim的Animation类,通过重写关键方法来实现气泡的上升、变大和透明度变化效果。
  • 操作系统缓存机制深度解析:从页缓存到内存映射,超越Redis的性能优化之道
  • 深智微:华润微官方授权代理商,如何让型号、库存交期与项目交付协同推进
  • 新用户福利,千问新用户福利怎么领,领取8元优惠券,附最新口令