commit 29f75f5eda44f26001c5aba660df4d5d1af854eb Author: Eden Kirin Date: Fri Jan 5 21:02:24 2024 +0100 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d74e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ diff --git a/config_pattern/Makefile b/config_pattern/Makefile new file mode 100644 index 0000000..860613c --- /dev/null +++ b/config_pattern/Makefile @@ -0,0 +1,2 @@ +run: + @go run main.go \ No newline at end of file diff --git a/config_pattern/go.mod b/config_pattern/go.mod new file mode 100644 index 0000000..c0f0ec5 --- /dev/null +++ b/config_pattern/go.mod @@ -0,0 +1,3 @@ +module patterns + +go 1.21.5 diff --git a/config_pattern/main.go b/config_pattern/main.go new file mode 100644 index 0000000..b4a9ad1 --- /dev/null +++ b/config_pattern/main.go @@ -0,0 +1,64 @@ +package main + +import "fmt" + +type SetFilterFunc func(options *FilterOptions) + +type FilterOptions struct { + Alive *bool + Id *int + ExternalId *string +} + +type Filter struct { + Options FilterOptions +} + +func (f *Filter) Dump() { + if f.Options.Alive != nil { + fmt.Printf("Alive: %v\n", *f.Options.Alive) + } + if f.Options.Id != nil { + fmt.Printf("Id: %v\n", *f.Options.Id) + } + if f.Options.ExternalId != nil { + fmt.Printf("ExternalId: %v\n", *f.Options.ExternalId) + } +} + +func WithAlive(options *FilterOptions) { + alive := true + options.Alive = &alive +} + +func WithId(id int) SetFilterFunc { + return func(options *FilterOptions) { + options.Id = &id + } +} + +func WithExternalId(externalId string) SetFilterFunc { + return func(options *FilterOptions) { + options.ExternalId = &externalId + } +} + +func NewFilter(opts ...SetFilterFunc) *Filter { + f := Filter{Options: FilterOptions{}} + for _, fn := range opts { + fn(&f.Options) + } + return &f +} + +func main() { + var f *Filter + + f = NewFilter() + f.Dump() + + fmt.Println("-------------------") + + f = NewFilter(WithId(123), WithAlive, WithExternalId("some-external-id")) + f.Dump() +} diff --git a/dependency_injection/Makefile b/dependency_injection/Makefile new file mode 100644 index 0000000..860613c --- /dev/null +++ b/dependency_injection/Makefile @@ -0,0 +1,2 @@ +run: + @go run main.go \ No newline at end of file diff --git a/dependency_injection/go.mod b/dependency_injection/go.mod new file mode 100644 index 0000000..c0f0ec5 --- /dev/null +++ b/dependency_injection/go.mod @@ -0,0 +1,3 @@ +module patterns + +go 1.21.5 diff --git a/dependency_injection/main.go b/dependency_injection/main.go new file mode 100644 index 0000000..2db7a64 --- /dev/null +++ b/dependency_injection/main.go @@ -0,0 +1,51 @@ +package main + +import "fmt" + +type Repository interface { + Save(user *User) error +} + +type User struct { + firstName string + lastName string + repository Repository +} + +func NewUser(firstName string, lastName string, repository Repository) *User { + return &User{ + firstName: firstName, + lastName: lastName, + repository: repository, + } +} + +func (u User) Save() { + u.repository.Save(&u) +} + +type DbRepository struct { + DbName string +} + +func (r DbRepository) Save(user *User) error { + fmt.Printf("Saving %s %s to DB name %s...\n", user.firstName, user.lastName, r.DbName) + return nil +} + +type ApiRepository struct { + Url string +} + +func (r ApiRepository) Save(user *User) error { + fmt.Printf("Saving %s %s to API url %s...\n", user.firstName, user.lastName, r.Url) + return nil +} + +func main() { + pero := NewUser("Pero", "Perić", &DbRepository{DbName: "pero_db"}) + pero.Save() + + mirko := NewUser("Mirko", "Mirković", &ApiRepository{Url: "http://pero.com"}) + mirko.Save() +}