mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-23 09:58:06 +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"
|
"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) {
|
lat, lng float32, err error) {
|
||||||
isValid := l.validateAddress(address)
|
isValid := l.validateAddress(address)
|
||||||
if !isValid {
|
if !isValid {
|
||||||
|
|
@ -22,7 +22,7 @@ func (l locator) getCoordinates1(ctx context.Context, address string) (
|
||||||
return 0, 0, nil
|
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) {
|
lat, lng float32, err error) {
|
||||||
isValid := l.validateAddress(address)
|
isValid := l.validateAddress(address)
|
||||||
if !isValid {
|
if !isValid {
|
||||||
|
|
@ -37,6 +37,21 @@ func (l locator) getCoordinates2(ctx context.Context, address string) (
|
||||||
return 0, 0, nil
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,13 +68,13 @@ func listing5() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
i = 1
|
i = 1
|
||||||
mutex.Unlock()
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
i = 2
|
i = 2
|
||||||
mutex.Unlock()
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,11 @@ func read2(r io.Reader) (int, error) {
|
||||||
wg.Add(n)
|
wg.Add(n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
for b := range ch {
|
for b := range ch {
|
||||||
v := task(b)
|
v := task(b)
|
||||||
atomic.AddInt64(&count, int64(v))
|
atomic.AddInt64(&count, int64(v))
|
||||||
}
|
}
|
||||||
wg.Done()
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
customer := Customer{}
|
||||||
|
_ = customer.UpdateAge1(-1)
|
||||||
|
_ = customer.UpdateAge2(-1)
|
||||||
|
_ = customer.UpdateAge3(-1)
|
||||||
|
}
|
||||||
|
|
||||||
type Customer struct {
|
type Customer struct {
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
id string
|
id string
|
||||||
|
|
@ -17,7 +23,7 @@ func (c *Customer) UpdateAge1(age int) error {
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
if age < 0 {
|
if age < 0 {
|
||||||
return errors.New("age should be positive")
|
return fmt.Errorf("age should be positive for customer %v", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.age = age
|
c.age = age
|
||||||
|
|
@ -26,7 +32,7 @@ func (c *Customer) UpdateAge1(age int) error {
|
||||||
|
|
||||||
func (c *Customer) UpdateAge2(age int) error {
|
func (c *Customer) UpdateAge2(age int) error {
|
||||||
if age < 0 {
|
if age < 0 {
|
||||||
return errors.New("age should be positive")
|
return fmt.Errorf("age should be positive for customer %v", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
|
@ -36,6 +42,18 @@ func (c *Customer) UpdateAge2(age int) error {
|
||||||
return nil
|
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 {
|
func (c *Customer) String() string {
|
||||||
c.mutex.RLock()
|
c.mutex.RLock()
|
||||||
defer c.mutex.RUnlock()
|
defer c.mutex.RUnlock()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,20 @@
|
||||||
package main
|
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 {
|
type Cache struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
@ -48,5 +62,5 @@ func (c *Cache) AverageBalance3() float64 {
|
||||||
for _, balance := range m {
|
for _, balance := range m {
|
||||||
sum += balance
|
sum += balance
|
||||||
}
|
}
|
||||||
return sum / float64(len(c.balances))
|
return sum / float64(len(m))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
listing1()
|
||||||
|
//listing2()
|
||||||
|
}
|
||||||
|
|
||||||
func listing1() {
|
func listing1() {
|
||||||
ticker := time.NewTicker(1000)
|
ticker := time.NewTicker(1000)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
// Do something
|
fmt.Println("tick")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -17,7 +25,7 @@ func listing2() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
// Do something
|
fmt.Println("tick")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,9 @@ func main() {
|
||||||
if err := listing2(); err != nil {
|
if err := listing2(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
if err := listing3(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event1 struct {
|
type Event1 struct {
|
||||||
|
|
@ -54,3 +57,35 @@ func listing2() error {
|
||||||
fmt.Println(string(b))
|
fmt.Println(string(b))
|
||||||
return nil
|
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
|
// +build integration
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue