惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
IT之家
IT之家
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
U
Unit 42
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cisco Blogs
S
Schneier on Security
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
博客园 - 聂微东
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
V2EX
L
Lohrmann on Cybersecurity
Latest news
Latest news
小众软件
小众软件
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
雷峰网
雷峰网
K
Kaspersky official blog
美团技术团队
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
Recorded Future
Recorded Future
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security

博客园 - ®Geovin Du Dream Park™

go: Functional Options Pattern go:Timing Functions Pattern python: Timing Functions Pattern go: Steady-State Pattern python: Steady-State Pattern go: Handshaking Pattern python: Handshaking Pattern python: Fail-Fast Pattern I go: Deadline Pattern python: Deadline Pattern go: Circuit-Breaker Pattern python: Circuit-Breaker Pattern go: Bulkheads Pattern python: Bulkheads Pattern go: Push & Pull Pattern python: Push & Pull Pattern python: Publish/Subscribe Pattern II go: Publish/Subscribe Pattern python: Publish/Subscribe Pattern go: Futures & Promises Pattern python: Futures & Promises Pattern go: Worker Pool Pattern go:Pipeline Pattern python: Worker Pool Pattern python: Pipeline Pattern go: Fan-Out Pattern python: Fan-Out Pattern go: Fan-In Pattern python: Fan-In Pattern Fan-In go: Producer Consumer  Pattern python: Producer Consumer Pattern go: Parallelism Pattern python: Parallelism Pattern go: Reactor Pattern python: Reactor Pattern go: Generators Pattern python: speech to text offline python: Generators Pattern go: Coroutines Pattern python:Coroutines Pattern go: Broadcast Pattern python: Broadcast Pattern python: Bounded Parallelism Pattern go: Bounded Parallelism Pattern python: N-Barrier Pattern go: N-Barrier Pattern python: Semaphore Pattern go: Semaphore Pattern python: Read-Write Lock Pattern go: Read-Write Lock Pattern python: Monitor Pattern go: Monitor Pattern python: Mutex Pattern go: Lock/Mutex Pattern Python: Condition Variable Pattern go:Condition Variable Pattern python: Registry Pattern python: Interpreter Pattern go: Interpreter Pattern go: Registry Pattern go: Memento Pattern go: Command Pattern go: State Pattern go:Template Method Pattern go: Strategy Pattern go: Visitor Pattern go: Observer Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy Pattern go:Decorator Pattern go: Facade Pattern go: Flyweight Pattern go: Composite Pattern go: Singleton Pattern go: Prototype Pattern go: Bridge Pattern go: Adapter Pattern go: Builder Pattern 密码进行加盐哈希 using CSharp,Python,Go,Java go: Abstract Factory Pattern go: Model,Interface,DAL ,Factory,BLL using mysql go: Simple Factory Pattern go: Factory Method Pattern go: goLang 在Windows环境搭建Go语言开发环境 CSharp: Parallel Extensions cpp: class python: 蝴蝶跟随鼠标 javascript: 中国历史人物热力分布图using echart javascript: 中国历史人物热力图 python: 初养龙虾微信纯文字自动回复using workBuddy python: object 入门 python: Factory Method Pattern python: Simple Factory Pattern python: Abstract Factory Pattern 区域化 代码 python: Null Object Pattern python: Builder Pattern python: Adapter Pattern
go: Fail-Fast Pattern
®Geovin Du Dream Park™ · 2026-06-30 · via 博客园 - ®Geovin Du Dream Park™
/*
# 版权所有  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))
}