课程: Hands-On Introduction: Go

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Sync package

Sync package

- Ghost Standard Library, includes package sync, which provides basic synchronization primitives including things like mutual exclusion locks, as well as handy features such as the Wait Group type, which helps us wait for a collection of goroutines to finish their work. As usual, let's get hands on with some examples. So, we're going to start by creating a list of our names to work with here. It's going to be a slice of strings, and we're going to initialize that with some values. All right. Now we're going to create a map to store the length of each of those names as an integer. Basically using the names as the keys for the map. So let's initialize that now. All right. We want to process each name in its own goroutine. So given our list of names, we pretty much already know how many goroutines we're going to launch. And that we'll actually need to wait for. In such cases, the…

内容