它很接近但不完全。我最终在此处提供了 Go 中的串行调度队列实现。它基本上是一个 go 例程,它阻塞 achannel类型func()并运行按顺序传递的函数。执行://Package serialqueue provides a serial queue for functions. //Queue items are processed in First In First Out (FIFO) order. package serialqueue//New returns a new serial queue.//Enqueue items like queueObj <- func() {doWork(data)}func New() chan func() { //create channel of type function var queue = make(chan func()) //spawn go routine to read and run functions in the channel go func() { for true { nextFunction := <-queue nextFunction() } }() return queue}用法:(演示以正确顺序写入字符串)//Package serialqueue provides provides tests for github.com/ansonl/serialqueue. package serialqueue_testimport ( "testing" "fmt" "sync" "github.com/ansonl/serialqueue" )func TestQueue(t *testing.T) { //Create new serial queue queue := serialqueue.New() //Number of times to loop var loops = 100 //Queue output will be added here var queueOutput string //WaitGroup for determining when queue output is finished var wg sync.WaitGroup //Create function to place in queue var printTest = func(i int) { queueOutput = fmt.Sprintf("%v%v",queueOutput, i) wg.Done() } //Add functions to queue var i int; for i=0;i<loops;i++ { wg.Add(1) t:=i queue <- func() {printTest(t)} } //Generate correct output var correctOutput string for i=0;i<loops;i++ { correctOutput = fmt.Sprintf("%v%v", correctOutput, i) } //Wait until all functions in queue are done wg.Wait() //Compare queue output with correct output if queueOutput != correctOutput { t.Errorf("Serial Queue produced %v, want %v", queueOutput, correctOutput); }}希望这可以帮助有同样问题的人!