Initial
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.vscode/
|
||||
2
config_pattern/Makefile
Normal file
2
config_pattern/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
run:
|
||||
@go run main.go
|
||||
3
config_pattern/go.mod
Normal file
3
config_pattern/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module patterns
|
||||
|
||||
go 1.21.5
|
||||
64
config_pattern/main.go
Normal file
64
config_pattern/main.go
Normal file
@ -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()
|
||||
}
|
||||
2
dependency_injection/Makefile
Normal file
2
dependency_injection/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
run:
|
||||
@go run main.go
|
||||
3
dependency_injection/go.mod
Normal file
3
dependency_injection/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module patterns
|
||||
|
||||
go 1.21.5
|
||||
51
dependency_injection/main.go
Normal file
51
dependency_injection/main.go
Normal file
@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user