I’m new to Golang. For practice I’m writing a simple app that can print elapsed seconds to the terminal, at intervals, but the user can type a number to change any of the intervals. I want to suspend any printing to stdout while the user is writing, so I have this:
go func() {
for {
select {
case moment := <-seconds:
if isUserWriting == false {
if moment%longInterval == 0 {
fmt.Println(longMessage, moment)
} else if moment%mediumInterval == 0 {
fmt.Println(mediumMessage, moment)
} else if moment%shortInterval == 0 {
fmt.Println(shortMessage, moment)
}
}
}
}
}()
isUserWriting
is a global variable. It’s initially false, but it can be set to true here:
case userText := <-userCh:
if (isUserWriting == false) {
showMenu()
isUserWriting = true
Once the user submits something, I have functions that try to set it back to false:
func changeLongMessage() {
response := getUserInput("Instead of 'bong' type some other text for the bong message.")
longMessage = response
isUserWriting = false
}
But this doesn’t work, in that printing to stdout does not resume.
Setting isUserWriting
does successfully stop the timer from printing seconds to stdout. But when functions set isUserWriting
to false, the messages from the timer do not resume. Why is that? I thought I understood global variables, but perhaps not.
I thought that setting isUserWriting
to false would cause the timer messages to again get printed to stdout. But this doesn’t seem to happen.