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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
A
About on SuperTechFans
腾讯CDC
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
I
InfoQ
博客园 - 【当耐特】
美团技术团队
GbyAI
GbyAI
量子位
宝玉的分享
宝玉的分享
爱范儿
爱范儿
有赞技术团队
有赞技术团队
博客园 - Franky
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志

博客园 - ®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 go: Fail-Fast 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: Coroutines Pattern
®Geovin Du Dream Park™ · 2026-06-10 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:09
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry.go
*/
package domain

type Jewelry struct {
	ID          string
	Name        string
	Material    string
	Status      string
	Certificate string
}

const (
	StatusPending    = "待采购"
	StatusPurchasing = "采购中"
	StatusPurchased  = "原料已到货"
	StatusChecking   = "质检中"
	StatusChecked    = "质检合格"
	StatusProcessing = "加工中"
	StatusProcessed  = "成品完成"
	StatusAuthing    = "鉴定中"
	StatusAuthed     = "已获证书"
	StatusSelling    = "销售中"
	StatusSold       = "已销售出库"
)




/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:10
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : config.go
*/
package config

var Delay = map[string]int{
	"Purchase": 2,
	"Check":    1,
	"Process":  3,
	"Auth":     2,
	"Sell":     1,
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:10
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : logger.go
*/
package core

import (
	"log"
	"os"
)

var Logger = log.New(os.Stdout, "[Jewelry] ", log.LstdFlags|log.Lmicroseconds)
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:11
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : purchase.go
*/
package service

import (
	"godesginpattern/coroutines/config"
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"time"
)

func Purchase(j *domain.Jewelry) {
	j.Status = domain.StatusPurchasing
	core.Logger.Printf("【采购】开始 %s | %s", j.ID, j.Name)
	time.Sleep(time.Duration(config.Delay["Purchase"]) * time.Second)
	j.Status = domain.StatusPurchased
	core.Logger.Printf("【采购】完成 %s", j.ID)
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:12
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : check.go
*/
package service

import (
	"godesginpattern/coroutines/config"
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"time"
)

func Check(j *domain.Jewelry) {
	j.Status = domain.StatusChecking
	core.Logger.Printf("【质检】开始 %s", j.ID)
	time.Sleep(time.Duration(config.Delay["Check"]) * time.Second)
	j.Status = domain.StatusChecked
	core.Logger.Printf("【质检】完成 %s", j.ID)
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:12
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : process.go
*/
package service

import (
	"godesginpattern/coroutines/config"
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"time"
)

func Process(j *domain.Jewelry) {
	j.Status = domain.StatusProcessing
	core.Logger.Printf("【加工】开始 %s", j.ID)
	time.Sleep(time.Duration(config.Delay["Process"]) * time.Second)
	j.Status = domain.StatusProcessed
	core.Logger.Printf("【加工】完成 %s", j.ID)
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:12
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : auth.go
*/
package service

import (
	"godesginpattern/coroutines/config"
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"time"
)

func Auth(j *domain.Jewelry) {
	j.Status = domain.StatusAuthing
	core.Logger.Printf("【鉴定】开始 %s", j.ID)
	time.Sleep(time.Duration(config.Delay["Auth"]) * time.Second)
	j.Certificate = "NGTC-" + j.ID + "-AUTH"
	j.Status = domain.StatusAuthed
	core.Logger.Printf("【鉴定】完成 %s | 证书: %s", j.ID, j.Certificate)
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:13
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : sell.go
*/
package service

import (
	"godesginpattern/coroutines/config"
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"time"
)

func Sell(j *domain.Jewelry) {
	j.Status = domain.StatusSelling
	core.Logger.Printf("【销售】开始 %s", j.ID)
	time.Sleep(time.Duration(config.Delay["Sell"]) * time.Second)
	j.Status = domain.StatusSold
	core.Logger.Printf("【销售】完成 %s", j.ID)
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:13
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : flow.go
*/
package workflow

import (
	"godesginpattern/coroutines/core"
	"godesginpattern/coroutines/domain"
	"godesginpattern/coroutines/service"
)

// 异步协程全流程(Go 原生 goroutine + channel)
func RunFlow(j *domain.Jewelry, done chan bool) {
	core.Logger.Printf("===== 启动流程 %s | %s =====", j.ID, j.Name)

	// 协程异步执行(可暂停、可恢复、非阻塞)
	service.Purchase(j)
	service.Check(j)
	service.Process(j)
	service.Auth(j)
	service.Sell(j)

	core.Logger.Printf("===== 全部完成 %s | 状态: %s =====", j.ID, j.Status)
	done <- true
} 

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Coroutines 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/10 21:15
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : coroutinesbll.go
*/
package bll

import (
	"godesginpattern/coroutines/domain"
	"godesginpattern/coroutines/workflow"
)

func CoroutinesMain() {
	// 创建3件珠宝(和Python版完全一样)
	jewelryList := []*domain.Jewelry{
		{ID: "J001", Name: "18K金钻石项链", Material: "黄金+钻石", Status: domain.StatusPending},
		{ID: "J002", Name: "冰种翡翠手镯", Material: "翡翠", Status: domain.StatusPending},
		{ID: "J003", Name: "铂金戒指", Material: "铂金", Status: domain.StatusPending},
	}

	// 异步协程并发执行(Go 高并发核心)
	done := make(chan bool, len(jewelryList))

	for _, j := range jewelryList {
		go workflow.RunFlow(j, done) // 启动协程!
	}

	// 等待全部完成
	for range jewelryList {
		<-done
	}

	close(done)
}

输出:

image

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)