Перейти к содержимому

Какие проблемы есть в коде? Как исправить? Должно вывестись в консоль &{0 true} &{1 true} &{4 true} &{5 true}.

security · cryptoWildberries / WB - 12не решено

Какие проблемы есть в коде? Как исправить? Должно вывестись в консоль &{0 true} &{1 true} &{4 true} &{5 true}.

type Agent struct {
ID int
Enabled bool
}
func (a Agent) Enable() {
a.Enabled = true
}
type Enabler interface {
Enable()
}
func main() {
agents := make([]*Agent, 0, 5)
for i := 0; i < 2; i++ {
agents = append(agents, &Agent{ID: i})
}
addThirdPartyAgents(agents)
pipe := make(chan Enabler)
var wg sync.WaitGroup
wg.Add(2)
go pipeSend(pipe, agents, &wg)
go pipeEnableProcess(pipe, &wg)
wg.Wait()
}
func addThirdPartyAgents(agents []*Agent) {
thirdParty := []*Agent{
{ID: 4},
{ID: 5},
}
agents = append(agents, thirdParty...)
}
func pipeSend(pipe chan Enabler, agents []*Agent, wg *sync.WaitGroup) {
defer wg.Done()
for _, а := range agents {
pipe <- a
}
close(pipe)
}
func pipeEnableProcess(pipe chan Enabler, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case a := <-pipe:
a.Enable()
dbWrite(a) // «Сохраняем» и печатаем
}
}
}
var dbWrite = func(a any) {
fmt.Println(a)
time.Sleep(time.Second * 1)
}

Источник: Wildberries / WB - 12

← Используя токен, пользователь может зак… · Все задачи · Безопасность · Разбиение большого файла на зашифрованн… →