diff --git a/06-functions-methods/44-side-effects-named-result-parameters/main.go b/06-functions-methods/44-side-effects-named-result-parameters/main.go index 8e0d3cd..862cffd 100644 --- a/06-functions-methods/44-side-effects-named-result-parameters/main.go +++ b/06-functions-methods/44-side-effects-named-result-parameters/main.go @@ -5,9 +5,9 @@ import ( "errors" ) -type locator struct{} +type loc struct{} -func (l locator) getCoordinates1(ctx context.Context, address string) ( +func (l loc) getCoordinates1(ctx context.Context, address string) ( lat, lng float32, err error) { isValid := l.validateAddress(address) if !isValid { @@ -22,7 +22,7 @@ func (l locator) getCoordinates1(ctx context.Context, address string) ( return 0, 0, nil } -func (l locator) getCoordinates2(ctx context.Context, address string) ( +func (l loc) getCoordinates2(ctx context.Context, address string) ( lat, lng float32, err error) { isValid := l.validateAddress(address) if !isValid { @@ -37,6 +37,21 @@ func (l locator) getCoordinates2(ctx context.Context, address string) ( return 0, 0, nil } -func (l locator) validateAddress(address string) bool { +func (l loc) getCoordinates3(ctx context.Context, address string) ( + lat, lng float32, err error) { + isValid := l.validateAddress(address) + if !isValid { + return 0, 0, errors.New("invalid address") + } + + if err = ctx.Err(); err != nil { + return + } + + // Get and return coordinates + return 0, 0, nil +} + +func (l loc) validateAddress(address string) bool { return true } diff --git a/08-concurrency-foundations/58-races/races/main.go b/08-concurrency-foundations/58-races/races/main.go index 1de30be..62ba3cd 100644 --- a/08-concurrency-foundations/58-races/races/main.go +++ b/08-concurrency-foundations/58-races/races/main.go @@ -68,13 +68,13 @@ func listing5() { go func() { mutex.Lock() + defer mutex.Unlock() i = 1 - mutex.Unlock() }() go func() { mutex.Lock() + defer mutex.Unlock() i = 2 - mutex.Unlock() }() } diff --git a/08-concurrency-foundations/59-workload-type/main.go b/08-concurrency-foundations/59-workload-type/main.go index 6cf4f12..174cf88 100644 --- a/08-concurrency-foundations/59-workload-type/main.go +++ b/08-concurrency-foundations/59-workload-type/main.go @@ -40,11 +40,11 @@ func read2(r io.Reader) (int, error) { wg.Add(n) for i := 0; i < n; i++ { go func() { + defer wg.Done() for b := range ch { v := task(b) atomic.AddInt64(&count, int64(v)) } - wg.Done() }() } diff --git a/09-concurrency-practice/68-string-formatting/main.go b/09-concurrency-practice/68-string-formatting/main.go index 305b372..69a19e4 100644 --- a/09-concurrency-practice/68-string-formatting/main.go +++ b/09-concurrency-practice/68-string-formatting/main.go @@ -1,11 +1,17 @@ package main import ( - "errors" "fmt" "sync" ) +func main() { + customer := Customer{} + _ = customer.UpdateAge1(-1) + _ = customer.UpdateAge2(-1) + _ = customer.UpdateAge3(-1) +} + type Customer struct { mutex sync.RWMutex id string @@ -17,7 +23,7 @@ func (c *Customer) UpdateAge1(age int) error { defer c.mutex.Unlock() if age < 0 { - return errors.New("age should be positive") + return fmt.Errorf("age should be positive for customer %v", c) } c.age = age @@ -26,7 +32,7 @@ func (c *Customer) UpdateAge1(age int) error { func (c *Customer) UpdateAge2(age int) error { if age < 0 { - return errors.New("age should be positive") + return fmt.Errorf("age should be positive for customer %v", c) } c.mutex.Lock() @@ -36,6 +42,18 @@ func (c *Customer) UpdateAge2(age int) error { return nil } +func (c *Customer) UpdateAge3(age int) error { + c.mutex.Lock() + defer c.mutex.Unlock() + + if age < 0 { + return fmt.Errorf("age should be positive for customer id %s", c.id) + } + + c.age = age + return nil +} + func (c *Customer) String() string { c.mutex.RLock() defer c.mutex.RUnlock() diff --git a/09-concurrency-practice/70-mutex-slices-maps/main.go b/09-concurrency-practice/70-mutex-slices-maps/main.go index 0f08706..662abdb 100644 --- a/09-concurrency-practice/70-mutex-slices-maps/main.go +++ b/09-concurrency-practice/70-mutex-slices-maps/main.go @@ -1,6 +1,20 @@ package main -import "sync" +import ( + "fmt" + "sync" +) + +func main() { + c := Cache{ + balances: make(map[string]float64), + } + c.AddBalance("1", 1.0) + c.AddBalance("2", 3.0) + fmt.Println(c.AverageBalance1()) + fmt.Println(c.AverageBalance2()) + fmt.Println(c.AverageBalance3()) +} type Cache struct { mu sync.Mutex @@ -48,5 +62,5 @@ func (c *Cache) AverageBalance3() float64 { for _, balance := range m { sum += balance } - return sum / float64(len(c.balances)) + return sum / float64(len(m)) } diff --git a/10-standard-lib/75-wrong-time-duration/main.go b/10-standard-lib/75-wrong-time-duration/main.go index c825036..b44d2ad 100644 --- a/10-standard-lib/75-wrong-time-duration/main.go +++ b/10-standard-lib/75-wrong-time-duration/main.go @@ -1,13 +1,21 @@ package main -import "time" +import ( + "fmt" + "time" +) + +func main() { + listing1() + //listing2() +} func listing1() { ticker := time.NewTicker(1000) for { select { case <-ticker.C: - // Do something + fmt.Println("tick") } } } @@ -17,7 +25,7 @@ func listing2() { for { select { case <-ticker.C: - // Do something + fmt.Println("tick") } } } diff --git a/10-standard-lib/77-json-handling/type-embedding/main.go b/10-standard-lib/77-json-handling/type-embedding/main.go index 181218b..970a7d7 100644 --- a/10-standard-lib/77-json-handling/type-embedding/main.go +++ b/10-standard-lib/77-json-handling/type-embedding/main.go @@ -13,6 +13,9 @@ func main() { if err := listing2(); err != nil { panic(err) } + if err := listing3(); err != nil { + panic(err) + } } type Event1 struct { @@ -54,3 +57,35 @@ func listing2() error { fmt.Println(string(b)) return nil } + +type Event3 struct { + ID int + time.Time +} + +func (e Event3) MarshalJSON() ([]byte, error) { + return json.Marshal( + struct { + ID int + Time time.Time + }{ + ID: e.ID, + Time: e.Time, + }, + ) +} + +func listing3() error { + event := Event3{ + ID: 1234, + Time: time.Now(), + } + + b, err := json.Marshal(event) + if err != nil { + return err + } + + fmt.Println(string(b)) + return nil +} diff --git a/11-testing/82-categorizing-tests/build-tags/db_test.go b/11-testing/82-categorizing-tests/build-tags/db_test.go index c71a06d..026ad1f 100644 --- a/11-testing/82-categorizing-tests/build-tags/db_test.go +++ b/11-testing/82-categorizing-tests/build-tags/db_test.go @@ -1,8 +1,10 @@ +//go:build integration // +build integration package db import ( + "os" "testing" )