My goal is to implement a simple pomodoro timer using Go: channels, goroutines.
I'm a newbie in Go world and have some misunderstanding about naming convention. I read a lot of Docker's code on GitHub and there I saw that people sometimes use very short names. These names are not clear for me (a man who comes from C++ world).
func main() {
app := pomodoro.NewPomodoro()
app.Start()
}
Give me a review about structure's name
type (
PomodoroManager struct {
dur Duration
interv Interval
ch Chanel
serv PomodoroService
}
Duration struct {
working int
shortBreak int
longBreak int
}
Interval struct {
shortInterval int
longInterval int
}
Chanel struct {
start chan bool
endShortBreak chan bool
endLongBreak chan bool
end chan bool
}
)
func NewPomodoro() *PomodoroManager {
dur := Duration{
working: 25,
shortBreak: 5,
longBreak: 15,
}
interv := Interval{
shortInterval: 1,
longInterval: 5,
}
ch := Chanel{
start: make(chan bool),
endShortBreak: make(chan bool),
endLongBreak: make(chan bool),
end: make(chan bool),
}
serv := PomodoroService{}
return &PomodoroManager{dur, interv, ch, serv}
}
func (p *PomodoroManager) Start() {
go p.serv.StartWorking(p.ch.start, p.dur.working)
go p.startServManager(p.ch.start, p.ch.endShortBreak, p.ch.endLongBreak, p.ch.end)
<-p.ch.end
}
func (p *PomodoroManager) startServManager(start, endBreak, endLong, end chan bool) {
for {
select {
case afterWorking := <-start:
_ = afterWorking
if p.interv.shortInterval == p.interv.longInterval {
go p.serv.StartLongBreak(endLong, p.dur.longBreak)
} else {
p.interv.shortInterval += 1
go p.serv.StartBreak(endBreak, p.dur.shortBreak)
}
case endShortRelax := <-endBreak:
_ = endShortRelax
go p.serv.StartWorking(start, p.dur.working)
case endLongRelax := <-endLong:
_ = endLongRelax
userAnswer := askUser()
if userAnswer == "Y" {
p.interv.shortInterval = 1
go p.serv.StartWorking(start, p.dur.working)
} else {
end <- true
}
}
}
}
func askUser() string {
fmt.Println("Do you want to coninue? Y/N")
var answer string
fmt.Scanln(&answer)
return answer
}
And Last component of the program.
import (
"os/exec"
"time"
)
type (
Service interface {
StartWorking(chan bool, int)
FinishBreak(chan bool, int)
FinishLongBreak(chan bool, int)
}
PomodoroService struct {
}
)
func (p *PomodoroService) StartWorking(start chan bool, duration int) {
exec.Command("say", "Start working").Output()
time.Sleep(time.Millisecond * time.Duration(duration))
exec.Command("say", "Time to Relax").Output()
start <- true
}
func (p *PomodoroService) StartBreak(endBreak chan bool, duration int) {
exec.Command("say", "Start short relax").Output()
time.Sleep(time.Millisecond * time.Duration(duration))
exec.Command("say", "Relax has finished, getting back to job").Output()
endBreak <- true
}
func (p *PomodoroService) StartLongBreak(endLong chan bool, duration int) {
exec.Command("say", "Start long relax").Output()
time.Sleep(time.Millisecond * time.Duration(duration))
exec.Command("say", "Long pause has finished").Output()
endLong <- true
}