go: Coroutines Pattern

项目结构:

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

 

posted @ 2026-06-10 21:22  ®Geovin Du Dream Park™  阅读(2)  评论(0)    收藏  举报