add go mutext

main
hau 2020-08-29 13:28:01 +07:00
parent 14c2afd04d
commit 35f86b8c87
2 changed files with 44 additions and 1 deletions

View File

@ -1,6 +1,6 @@
# [strings](https://golang.org/pkg/strings/)
Replace list of strings by using Replacer:
Replace list of strings by using `Replacer`:
```go
template := "My city is {city} and my country is {country}"

43
Development/Go/sync.md Normal file
View File

@ -0,0 +1,43 @@
# [sync](https://golang.org/pkg/sync/)
Use `Mutext` to prevent accessing variable from different goroutine at the same time:
```go
type foo struct {
mu sync.Mutext
bar map[string]string
}
func (f *foo) doSomething {
f.mu.Lock()
defer f.mu.Unlock()
// do the thing
}
```
Use `RWMutex` if there are many readers, and few writers:
```go
type foo struct {
mu sync.RWMutext
bar map[string]string
}
func (f *foo) read {
f.mu.RLock()
defer f.mu.RUnlock()
// read
}
func (f *foo) write {
f.mu.Lock()
defer f.mu.Unlock()
// write
}
```