Lessons from building a Pomodoro timer in GO
Over the last couple of weeks, I have been trying to get the basics of go in my free time. To do so, I decided to build a terminal-based Pomodoro timer. If you are not familiar with the Pomodoro technique, here you can find more about it.
The code I will speak of in this article can be found here, if you have go installed you can build it and use it/test it to your heart’s content. If not, it is easy to do!
I shall not go into too much of the code, but some snippets will feature in this article
Lessons
Go is surprisingly easy to start. If you have never tried it and want to learn a new programming language, I recommend trying your hand at it. Just follow the documentation until you feel comfortable and then invent a dummy project to try it out :)
When trying out a new language, don’t bother too much on doing things ‘right’, get first a feel for it and learn the ‘proper’ way to do it at a later date. Do not get bogged down with technicalities.
If you are building something to be used on the terminal, you will print a lot of text!
fmt.Println(“How long should the work time be for this block?”)tempWorkTime, _ := reader.ReadString(‘\n’)workTime, err = strconv.Atoi(strings.TrimSuffix(tempWorkTime, “\n”))fmt.Printf(“Cool, let’s set a work session to %v minutes \n”, workTime)fmt.Println(“How long should the break time be for this block?”)tempBreakTime, _ := reader.ReadString(‘\n’)breakTime, err = strconv.Atoi(strings.TrimSuffix(tempBreakTime, “\n”))fmt.Printf(“Cool, let’s set a break after your session of %v minutes \n”, breakTime)
Text that replaces the previous line is really useful:
fmt.Printf(“\033[2K\rWhat should we work on this block ? \n”)// \033[2K\r => This part replaces the previous line on your terminal
Writing to a file in go is surprisingly easy (granted I never write to a file in any language so it might be easy in all of them):
func writeTaskLog(message string) (status bool) { if _, err := os.Stat(“log/task_log.txt”); err == nil { f, err := os.OpenFile(“log/task_log.txt”, os.O_APPEND|os.O_WRONLY, 0644) check(err) defer f.Close() f.WriteString(“\n-” + message) return true } os.Mkdir(“log”, 0777) err := ioutil.WriteFile(“log/task_log.txt”, []byte(“- “+message), 0777) check(err) return true}
And after the ‘basics’?
The next obvious step would be to rewrite the same tool but follow best practices and test the code. By the way, testing is built-in rather nicely in GO.
The next ‘step’ would also involve having a UI for the tool.
Conclusion
Go is ‘tougher’ than ruby, which is the language I am most comfortable with. But it is a swell choice to start learning the intricacies of lower-level languages.
Learning a new language is always fun, and a Pomodoro timer has enough complexity to make it fun while being relatively simple when you get down to it. It will probably be my build of choice when trying out a new language.