I'm writing a bot to run some commands in parallel and at the same time run the bots in parallel, but I'm having trouble starting and pausing functions.
Below I'll leave an example I set up. It was expected that one of the bots would continue to run and others would stop, but all would end up running.
Could someone explain to me why, when using the startbot()
command, it does not get bool?
package main
import (
"log"
"time"
)
type botBase struct {
isEnabled bool
}
func (b *botBase) startFunctionX() {
b.isEnabled = true
}
func (b *botBase) pauseFunctionX() {
b.isEnabled = false
}
func (b botBase) runCommandX() {
for {
if b.isEnabled {
log.Print("running...")
} else {
log.Print("paused...")
}
time.Sleep(1 * time.Second)
}
}
type bot struct {
botBase
//other stuffs
}
func (b bot) runAllCommands() {
go b.runCommandX()
//wait parallels commands
for{
time.Sleep(10 * time.Hour)
}
}
type bots struct {
List []bot
}
func (b *bots) loadListDB() {
b1 := bot{}
b1.isEnabled = false
b2 := bot{}
b2.isEnabled = false
b.List = []bot{b1, b2}
}
var myBots bots
func main() {
myBots.loadListDB()
for _, b := range myBots.List {
b.startFunctionX()
go b.runAllCommands()
}
//control stop and start bots
log.Print("expected true = ", myBots.List[0].isEnabled)
myBots.List[0].pauseFunctionX()
log.Print("expected false = ", myBots.List[0].isEnabled)
//wait bots parallels
for {
time.Sleep(10 * time.Hour)
}
}