From 0102eaf7503f247083dfa843ac6b1a65f2d9c188 Mon Sep 17 00:00:00 2001 From: teivah Date: Thu, 27 Jan 2022 00:06:23 +0100 Subject: [PATCH] 3P updates --- .../1-variable-shadowing/main.go | 16 +++++++++ .../3-init-functions/db/main.go | 11 ++++++ .../restricting-behavior/main.go | 35 +++++++++++++++++++ .../6-interface-producer/client/client.go | 7 ++++ .../6-interface-producer/store/store.go | 12 +++++++ .../9-generics/main.go | 28 +++++++++++---- 05-strings/39-string-concat/main.go | 22 ++++++------ 05-strings/41-substring-memory-leak/main.go | 11 ++++++ 8 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 02-code-project-organization/5-interface-pollution/restricting-behavior/main.go create mode 100644 02-code-project-organization/6-interface-producer/client/client.go create mode 100644 02-code-project-organization/6-interface-producer/store/store.go diff --git a/02-code-project-organization/1-variable-shadowing/main.go b/02-code-project-organization/1-variable-shadowing/main.go index fd59a20..16187a2 100644 --- a/02-code-project-organization/1-variable-shadowing/main.go +++ b/02-code-project-organization/1-variable-shadowing/main.go @@ -64,6 +64,22 @@ func listing3() error { return nil } +func listing4() error { + var client *http.Client + var err error + if tracing { + client, err = createClientWithTracing() + } else { + client, err = createDefaultClient() + } + if err != nil { + return err + } + + _ = client + return nil +} + var tracing bool func createClientWithTracing() (*http.Client, error) { diff --git a/02-code-project-organization/3-init-functions/db/main.go b/02-code-project-organization/3-init-functions/db/main.go index 24c11a2..fe75762 100644 --- a/02-code-project-organization/3-init-functions/db/main.go +++ b/02-code-project-organization/3-init-functions/db/main.go @@ -20,3 +20,14 @@ func init() { } db = d } + +func createClient(dataSourceName string) (*sql.DB, error) { + db, err := sql.Open("mysql", dataSourceName) + if err != nil { + return nil, err + } + if err = db.Ping(); err != nil { + return nil, err + } + return db, nil +} diff --git a/02-code-project-organization/5-interface-pollution/restricting-behavior/main.go b/02-code-project-organization/5-interface-pollution/restricting-behavior/main.go new file mode 100644 index 0000000..b98cf48 --- /dev/null +++ b/02-code-project-organization/5-interface-pollution/restricting-behavior/main.go @@ -0,0 +1,35 @@ +package main + +type IntConfig struct { + value int +} + +func (c *IntConfig) Get() int { + return c.value +} + +func (c *IntConfig) Set(value int) { + c.value = value +} + +type intConfigGetter interface { + Get() int +} + +type Foo struct { + threshold intConfigGetter +} + +func NewFoo(threshold intConfigGetter) Foo { + return Foo{threshold: threshold} +} + +func (f Foo) Bar() { + threshold := f.threshold.Get() + _ = threshold +} + +func main() { + foo := NewFoo(&IntConfig{value: 42}) + foo.Bar() +} diff --git a/02-code-project-organization/6-interface-producer/client/client.go b/02-code-project-organization/6-interface-producer/client/client.go new file mode 100644 index 0000000..1b205b7 --- /dev/null +++ b/02-code-project-organization/6-interface-producer/client/client.go @@ -0,0 +1,7 @@ +package client + +import "github.com/teivah/100-go-mistakes/02-code-project-organization/6-interface-producer/store" + +type customersGetter interface { + GetAllCustomers() ([]store.Customer, error) +} diff --git a/02-code-project-organization/6-interface-producer/store/store.go b/02-code-project-organization/6-interface-producer/store/store.go new file mode 100644 index 0000000..75d7a2f --- /dev/null +++ b/02-code-project-organization/6-interface-producer/store/store.go @@ -0,0 +1,12 @@ +package store + +type Storer interface { + StoreCustomer(customer Customer) error + GetCustomer(id string) (Customer, error) + UpdateCustomer(customer Customer) error + GetAllCustomers() ([]Customer, error) + GetCustomersWithoutContract() ([]Customer, error) + GetCustomersWithNegativeBalance() ([]Customer, error) +} + +type Customer struct{} diff --git a/02-code-project-organization/9-generics/main.go b/02-code-project-organization/9-generics/main.go index 3d0657b..27e376f 100644 --- a/02-code-project-organization/9-generics/main.go +++ b/02-code-project-organization/9-generics/main.go @@ -1,6 +1,9 @@ package main -import "fmt" +import ( + "fmt" + "sort" +) func getKeys(m any) ([]any, error) { switch t := m.(type) { @@ -44,11 +47,22 @@ func (n *Node[T]) Add(next *Node[T]) { n.next = next } -type sliceFn[T any] struct { - s []T - compare func(T, T) bool +type SliceFn[T any] struct { + S []T + Compare func(T, T) bool } -func (s sliceFn[T]) Len() int { return len(s.s) } -func (s sliceFn[T]) Less(i, j int) bool { return s.compare(s.s[i], s.s[j]) } -func (s sliceFn[T]) Swap(i, j int) { s.s[i], s.s[j] = s.s[j], s.s[i] } \ No newline at end of file +func (s SliceFn[T]) Len() int { return len(s.S) } +func (s SliceFn[T]) Less(i, j int) bool { return s.Compare(s.S[i], s.S[j]) } +func (s SliceFn[T]) Swap(i, j int) { s.S[i], s.S[j] = s.S[j], s.S[i] } + +func main() { + s := SliceFn[int]{ + S: []int{3, 2, 1}, + Compare: func(a, b int) bool { + return a < b + }, + } + sort.Sort(s) + fmt.Println(s.S) +} \ No newline at end of file diff --git a/05-strings/39-string-concat/main.go b/05-strings/39-string-concat/main.go index f689211..77e69ca 100644 --- a/05-strings/39-string-concat/main.go +++ b/05-strings/39-string-concat/main.go @@ -2,32 +2,32 @@ package main import "strings" -func concat1(ids []string) string { +func concat1(values []string) string { s := "" - for _, id := range ids { - s += id + for _, value := range values { + s += value } return s } -func concat2(ids []string) string { +func concat2(values []string) string { sb := strings.Builder{} - for _, id := range ids { - _, _ = sb.WriteString(id) + for _, value := range values { + _, _ = sb.WriteString(value) } return sb.String() } -func concat3(ids []string) string { +func concat3(values []string) string { total := 0 - for i := 0; i < len(ids); i++ { - total += len(ids[i]) + for i := 0; i < len(values); i++ { + total += len(values[i]) } sb := strings.Builder{} sb.Grow(total) - for _, id := range ids { - _, _ = sb.WriteString(id) + for _, value := range values { + _, _ = sb.WriteString(value) } return sb.String() } diff --git a/05-strings/41-substring-memory-leak/main.go b/05-strings/41-substring-memory-leak/main.go index b2cb831..cfc086b 100644 --- a/05-strings/41-substring-memory-leak/main.go +++ b/05-strings/41-substring-memory-leak/main.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "strings" ) func main() { @@ -37,6 +38,16 @@ func (s store) handleLog2(log string) error { return nil } +func (s store) handleLog3(log string) error { + if len(log) < 36 { + return errors.New("log is not correctly formatted") + } + uuid := string(strings.Clone(log[:36])) + s.store(uuid) + // Do something + return nil +} + func (s store) store(uuid string) { // ... }