range字符串,使用goroutine打印
for _, v := range str { go func() { fmt.Println(string(v)) }() }
輸出:
5
5
5
5
5
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
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{}{} } }
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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
標(biāo)簽:蘭州 吐魯番 汕頭 銅川 雞西 重慶 梅河口 欽州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang goroutine順序輸出方式》,本文關(guān)鍵詞 golang,goroutine,順序,輸出,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。