package main
import (
"fmt"
"strings"
"sync"
)
var initString = "hello, world."
var stringLength = len([]byte(initString))
var ch chan string = make(chan string, stringLength)
var finalString string = ""
func main() {
wg := sync.WaitGroup{}
for i := 0; i < stringLength; i++ {
wg.Add(2)
go uppercase(string([]byte(initString)[i]), wg)
go addString(wg)
wg.Wait()
}
fmt.Println(finalString)
}
func uppercase(letter string, wg sync.WaitGroup) {
ch <- strings.ToUpper(letter)
wg.Done()
}
func addString(wg sync.WaitGroup) {
letter := <-ch
finalString += letter
wg.Done()
}
死锁
最新推荐文章于 2024-09-22 15:21:09 发布