-
Notifications
You must be signed in to change notification settings - Fork 3
/
switch.go
42 lines (33 loc) · 777 Bytes
/
switch.go
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
// Copyright (c) 2022 Dmitry Tkachenko (tkachenkodmitryv@gmail.com)
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package care
import (
"sync/atomic"
)
// Switch is a switch feature.
type Switch interface {
// TurnOn - turns on the feature.
TurnOn()
// TurnOff - turns off the feature.
TurnOff()
// IsTurnedOn - returns a state of the feature (turned on/off).
IsTurnedOn() bool
}
type switchImpl struct {
isTurnedOn atomic.Bool
}
func (s *switchImpl) TurnOn() {
s.isTurnedOn.Store(true)
}
func (s *switchImpl) TurnOff() {
s.isTurnedOn.Store(false)
}
func (s *switchImpl) IsTurnedOn() bool {
return s.isTurnedOn.Load()
}
func newSwitch() Switch {
inst := switchImpl{}
inst.TurnOn()
return &inst
}