3P updates

This commit is contained in:
teivah 2022-01-27 00:06:23 +01:00
parent 1a77876c3f
commit 0102eaf750
No known key found for this signature in database
GPG key ID: 07E4A13FB8F8DF63
8 changed files with 124 additions and 18 deletions

View file

@ -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) {

View file

@ -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
}

View file

@ -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()
}

View file

@ -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)
}

View file

@ -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{}

View file

@ -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] }
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)
}

View file

@ -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()
}

View file

@ -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) {
// ...
}