Advertisement
Thanakom

Sync.Mutex

Apr 18th, 2021
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.37 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type SafeCounter struct {
  8.     v  map[string]int
  9. }
  10.  
  11. func (c *SafeCounter) Inc(key string) {
  12.     c.v[key]++
  13. }
  14.  
  15. func (c *SafeCounter) Value(key string) int {
  16.     return c.v[key]
  17. }
  18.  
  19. func main() {
  20.     c := SafeCounter{v: make(map[string]int)}
  21.     for i := 0; i < 1000; i++ {
  22.         go c.Inc("somekey")
  23.     }
  24.    
  25.     fmt.Println(c.Value("somekey"))
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement