Go supports mutex with using sync.mutex
and its two methods - lock()
and unlock()
Example
Exercise: Make a web-crawler
Modify the Crawl
function to fetch URLs in parallel without fetching the same URL twice.
Hint: you can keep a cache of the URLs that have been fetched on a map, but maps alone are not safe for concurrent use!
(base) pulakesh@Pulakeshs-MacBook-Air Day-8 % go run web-crawler.go
found: <https://golang.org/> "The Go Programming Language"
[<https://golang.org/pkg/> <https://golang.org/cmd/>]
not found: <https://golang.org/cmd/>
found: <https://golang.org/pkg/> "Packages"
[<https://golang.org/> <https://golang.org/cmd/> <https://golang.org/pkg/fmt/> <https://golang.org/pkg/os/>]
found: <https://golang.org/pkg/os/> "Package os"
[<https://golang.org/> <https://golang.org/pkg/>]
found: <https://golang.org/pkg/fmt/> "Package fmt"
[<https://golang.org/> <https://golang.org/pkg/>]