Can I lock using specific values in Go?
问题 In answering another question I wrote a little struct using sync.Map to cache API requests. type PostManager struct { sync.Map } func (pc PostManager) Fetch(id int) Post { post, ok := pc.Load(id) if ok { fmt.Printf("Using cached post %v\n", id) return post.(Post) } fmt.Printf("Fetching post %v\n", id) post = pc.fetchPost(id) pc.Store(id, post) return post.(Post) } Unfortunately, if two goroutines both fetch the same uncached Post at the same time, both will make a request. var postManager