mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-25 02:46:53 +08:00
3P updates
This commit is contained in:
parent
1a77876c3f
commit
0102eaf750
8 changed files with 124 additions and 18 deletions
|
|
@ -64,6 +64,22 @@ func listing3() error {
|
||||||
return nil
|
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
|
var tracing bool
|
||||||
|
|
||||||
func createClientWithTracing() (*http.Client, error) {
|
func createClientWithTracing() (*http.Client, error) {
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,14 @@ func init() {
|
||||||
}
|
}
|
||||||
db = d
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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{}
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
func getKeys(m any) ([]any, error) {
|
func getKeys(m any) ([]any, error) {
|
||||||
switch t := m.(type) {
|
switch t := m.(type) {
|
||||||
|
|
@ -44,11 +47,22 @@ func (n *Node[T]) Add(next *Node[T]) {
|
||||||
n.next = next
|
n.next = next
|
||||||
}
|
}
|
||||||
|
|
||||||
type sliceFn[T any] struct {
|
type SliceFn[T any] struct {
|
||||||
s []T
|
S []T
|
||||||
compare func(T, T) bool
|
Compare func(T, T) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s sliceFn[T]) Len() int { return len(s.s) }
|
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]) 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]) 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)
|
||||||
|
}
|
||||||
|
|
@ -2,32 +2,32 @@ package main
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
func concat1(ids []string) string {
|
func concat1(values []string) string {
|
||||||
s := ""
|
s := ""
|
||||||
for _, id := range ids {
|
for _, value := range values {
|
||||||
s += id
|
s += value
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func concat2(ids []string) string {
|
func concat2(values []string) string {
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
for _, id := range ids {
|
for _, value := range values {
|
||||||
_, _ = sb.WriteString(id)
|
_, _ = sb.WriteString(value)
|
||||||
}
|
}
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func concat3(ids []string) string {
|
func concat3(values []string) string {
|
||||||
total := 0
|
total := 0
|
||||||
for i := 0; i < len(ids); i++ {
|
for i := 0; i < len(values); i++ {
|
||||||
total += len(ids[i])
|
total += len(values[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
sb.Grow(total)
|
sb.Grow(total)
|
||||||
for _, id := range ids {
|
for _, value := range values {
|
||||||
_, _ = sb.WriteString(id)
|
_, _ = sb.WriteString(value)
|
||||||
}
|
}
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -37,6 +38,16 @@ func (s store) handleLog2(log string) error {
|
||||||
return nil
|
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) {
|
func (s store) store(uuid string) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue