欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang goroutine順序輸出方式

 更新時間:2021年04月29日 11:46:48   作者:浩聞城  
這篇文章主要介紹了golang goroutine順序輸出方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

range字符串,使用goroutine打印

因為goroutine隨機執(zhí)行

for _, v := range str {
        go func() {
            fmt.Println(string(v))
        }()
}

輸出:

5

5

5

5

5

可以使用chan順序輸出

    for  _, c := range str{
        ch := make(chan rune)
        go func(ch <-chan rune) {
            key := <-ch
            fmt.Println(string(key))
        }(ch)
        ch <- c
    }

輸出:

1

2

3

4

5

補充:golang goroutine順序循環(huán)打印ABC

分別使用sync.WaitGroup和context

使用sync.WaitGroup, 可控制循環(huán)次數(shù)

package main
import (
	"fmt"
	"sync"
)
//控制循環(huán)次數(shù)
var count = 5
func main() {
	wg := sync.WaitGroup{}
	chanA := make(chan struct{}, 1)
	chanB := make(chan struct{}, 1)
	chanC := make(chan struct{}, 1)
	chanA <- struct{}{}
	wg.Add(3)
	go printA(&wg, chanA, chanB)
	go printB(&wg, chanB, chanC)
	go printC(&wg, chanC, chanA)
	wg.Wait()
}
func printA(wg *sync.WaitGroup, chanA, chanB chan struct{}) {
	defer wg.Done()
	for i := 0; i < count; i++ {
		<-chanA
		fmt.Println("A")
		chanB <- struct{}{}
	}
}
func printB(wg *sync.WaitGroup, chanB, chanC chan struct{}) {
	defer wg.Done()
	for i := 0; i < count; i++ {
		<-chanB
		fmt.Println("B")
		chanC <- struct{}{}
	}
}
func printC(wg *sync.WaitGroup, chanC, chanA chan struct{}) {
	defer wg.Done()
	for i := 0; i < count; i++ {
		<-chanC
		fmt.Println("C")
		chanA <- struct{}{}
	}
}

使用context.WithCancel,通過time.Sleep控制打印數(shù)量

package main
import (
	"context"
	"fmt"
	"time"
)
func main() {
	chanA := make(chan struct{}, 1)
	chanB := make(chan struct{}, 1)
	chanC := make(chan struct{}, 1)
	chanA <- struct{}{}
	ctx1, cancel1 := context.WithCancel(context.Background())
	ctx2, cancel2 := context.WithCancel(context.Background())
	ctx3, cancel3 := context.WithCancel(context.Background())
	go printA(ctx1, chanA, chanB)
	go printB(ctx2, chanB, chanC)
	go printC(ctx3, chanC, chanA)
	time.Sleep(100 * time.Microsecond)
	cancel1()
	cancel2()
	cancel3()
}
func printA(ctx context.Context, chanA, chanB chan struct{}) {
	for {
		select {
		case <-ctx.Done():
			fmt.Println("cancel by parent") // 不會輸出
			return
		case <-chanA:
			fmt.Println("A")
			chanB <- struct{}{}
		}
	}
}
func printB(ctx context.Context, chanB, chanC chan struct{}) {
	for {
		select {
		case <-ctx.Done():
			fmt.Println("cancel by parent") // 不會輸出
			return
		case <-chanB:
			fmt.Println("B")
			chanC <- struct{}{}
		}
	}
}
func printC(ctx context.Context, chanC, chanA chan struct{}) {
	for {
		select {
		case <-ctx.Done():
			fmt.Println("cancel by parent") // 不會輸出
			return
		case <-chanC:
			fmt.Println("C")
			chanA <- struct{}{}
		}
	}
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • go中make用法及常見的一些坑

    go中make用法及常見的一些坑

    golang分配內(nèi)存主要有內(nèi)置函數(shù)new和make,下面這篇文章主要給大家介紹了關(guān)于go中make用法及常見的一些坑,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Golang小數(shù)操作指南之判斷小數(shù)點位數(shù)與四舍五入

    Golang小數(shù)操作指南之判斷小數(shù)點位數(shù)與四舍五入

    這篇文章主要給大家介紹了關(guān)于Golang小數(shù)操作指南之判斷小數(shù)點位數(shù)與四舍五入的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-03-03
  • Go語言實現(xiàn)JSON解析的神器詳解

    Go語言實現(xiàn)JSON解析的神器詳解

    php轉(zhuǎn)go是大趨勢,越來越多公司的php服務(wù)都在用go進行重構(gòu),重構(gòu)過程中,會發(fā)現(xiàn)php的json解析操作是真的香。本文和大家分享了一個Go語言實現(xiàn)JSON解析的神器,希望對大家有所幫助
    2023-01-01
  • Go常問的一些面試題匯總(附答案)

    Go常問的一些面試題匯總(附答案)

    通常我們?nèi)ッ嬖嚳隙〞行┎诲e的Golang的面試題目的,所以總結(jié)下,讓其他Golang開發(fā)者也可以查看到,同時也用來檢測自己的能力和提醒自己的不足之處,這篇文章主要給大家介紹了關(guān)于Go常問的一些面試題以及答案的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • 淺析Go語言bitset的實現(xiàn)原理

    淺析Go語言bitset的實現(xiàn)原理

    bitset包是一個將非負整數(shù)映射到布爾值的位的集合,這篇文章主要通過開源包bitset來為大家分析一下位集合的設(shè)計和實現(xiàn),感興趣的可以學(xué)習(xí)一下
    2023-08-08
  • 利用Golang解析json數(shù)據(jù)的方法示例

    利用Golang解析json數(shù)據(jù)的方法示例

    Go提供了原生的JSON庫,并且與語言本身有效的集成在了一起。下面這篇文章將給大家介紹關(guān)于利用Golang解析json數(shù)據(jù)的方法,文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Go語言中你所不知道的位操作用法

    Go語言中你所不知道的位操作用法

    位運算可能在平常的編程中使用的并不多,但涉及到底層優(yōu)化,一些算法及源碼可能會經(jīng)常遇見。下面這篇文章主要給大家介紹了關(guān)于Go語言中你所不知道的位操作用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-12-12
  • Go語言異常處理案例解析

    Go語言異常處理案例解析

    這篇文章主要介紹了Go語言異常處理案例解析,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • golang常用庫之字段參數(shù)驗證庫-validator使用詳解

    golang常用庫之字段參數(shù)驗證庫-validator使用詳解

    這篇文章主要介紹了golang常用庫:字段參數(shù)驗證庫-validator使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借價值,需要的朋友可以參考下
    2020-10-10
  • Golang channel關(guān)閉的實現(xiàn)示例

    Golang channel關(guān)閉的實現(xiàn)示例

    channel關(guān)閉不當(dāng)或不關(guān)閉會引發(fā)很多問題,本文主要介紹了Golang channel關(guān)閉的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01

最新評論