Chaining
This commit is contained in:
2
chaining/Makefile
Normal file
2
chaining/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
run:
|
||||||
|
@go run main.go
|
||||||
3
chaining/go.mod
Normal file
3
chaining/go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module patterns
|
||||||
|
|
||||||
|
go 1.21.5
|
||||||
53
chaining/main.go
Normal file
53
chaining/main.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Filter struct {
|
||||||
|
alive *bool
|
||||||
|
id *int
|
||||||
|
externalId *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFilter() *Filter {
|
||||||
|
return &Filter{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Filter) Dump() {
|
||||||
|
fmt.Println("Filter non-nil options:")
|
||||||
|
if f.alive != nil {
|
||||||
|
fmt.Printf(" Alive: %v\n", *f.alive)
|
||||||
|
}
|
||||||
|
if f.id != nil {
|
||||||
|
fmt.Printf(" Id: %v\n", *f.id)
|
||||||
|
}
|
||||||
|
if f.externalId != nil {
|
||||||
|
fmt.Printf(" ExternalId: %v\n", *f.externalId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Filter) Alive(alive bool) *Filter {
|
||||||
|
f.alive = &alive
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Filter) Id(id int) *Filter {
|
||||||
|
f.id = &id
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Filter) ExternalId(externalId string) *Filter {
|
||||||
|
f.externalId = &externalId
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var f *Filter
|
||||||
|
|
||||||
|
f = NewFilter().ExternalId("some-external-id")
|
||||||
|
f.Dump()
|
||||||
|
|
||||||
|
fmt.Println("-------------------")
|
||||||
|
|
||||||
|
f = NewFilter().Id(123).Alive(true).ExternalId("some-external-id")
|
||||||
|
f.Dump()
|
||||||
|
}
|
||||||
@ -54,7 +54,7 @@ func NewFilter(opts ...SetFilterFunc) *Filter {
|
|||||||
func main() {
|
func main() {
|
||||||
var f *Filter
|
var f *Filter
|
||||||
|
|
||||||
f = NewFilter()
|
f = NewFilter(WithExternalId("some-external-id"))
|
||||||
f.Dump()
|
f.Dump()
|
||||||
|
|
||||||
fmt.Println("-------------------")
|
fmt.Println("-------------------")
|
||||||
|
|||||||
Reference in New Issue
Block a user