-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (55 loc) · 1.14 KB
/
main.go
File metadata and controls
71 lines (55 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import "fmt"
const (
defaultSpec = "lifestyle"
defaultAwdOption = false
)
//Car store new car instance
type Car struct {
HasLeatherInterior bool
HasAWD bool
HasPowerFrontSeat bool
Specs string
}
//CarOption store CarOption
type CarOption func(*Car)
//WithLeatherInterior modify *Car behaviour
func WithLeatherInterior() CarOption {
return func(c *Car) {
c.HasLeatherInterior = true
}
}
//WithPowerFrontSeat modify *Car behaviour
func WithPowerFrontSeat() CarOption {
return func(c *Car) {
c.HasPowerFrontSeat = true
}
}
//WithAWD modify *Car behaviour
func WithAWD() CarOption {
return func(c *Car) {
c.HasAWD = true
}
}
//WithSpecs modify *Car behaviour
func WithSpecs(s string) CarOption {
return func(c *Car) {
c.Specs = s
}
}
//NewCar create new Car instance
func NewCar(opts ...CarOption) *Car {
//create Car instance with default options
car := &Car{
HasAWD: defaultAwdOption,
Specs: defaultSpec,
}
for _, opt := range opts {
opt(car)
}
return car
}
func main() {
car := NewCar(WithLeatherInterior(), WithSpecs("prestige"), WithPowerFrontSeat())
fmt.Println(car)
}