mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
3P
This commit is contained in:
parent
785979c87b
commit
727ecfbc94
8 changed files with 107 additions and 15 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue