100-go-mistakes/09-concurrency-practice/68-string-formatting/main.go
2021-12-27 15:57:20 +01:00

43 lines
613 B
Go

package main
import (
"errors"
"fmt"
"sync"
)
type Customer struct {
mutex sync.RWMutex
id string
age int
}
func (c *Customer) UpdateAge1(age int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if age < 0 {
return errors.New("age should be positive")
}
c.age = age
return nil
}
func (c *Customer) UpdateAge2(age int) error {
if age < 0 {
return errors.New("age should be positive")
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.age = age
return nil
}
func (c *Customer) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
return fmt.Sprintf("id %s, age %d", c.id, c.age)
}