Answer the question
In order to leave comments, you need to log in
Goutins, channels, not fully working out, how to figure it out?
Greetings, I just started learning go and immediately got stuck on channels and goroutines. I'll give you the code right now
func main() {
chanUrls := make(chan string, 100)
chanRes := make(chan http.Response, 100)
zipChan := make(chan Gallery, 100)
for url := range Urls{
chanUrls <- url
}
go httpGet(chanUrls, chanRes)
go parser(chanRes, zipChan)
for index := 0; index < 2; index++ {
go zipMaker(zipChan)
}
defer close(zipChan)
for range zipChan {
}
}
func httpGet(chanUrls <-chan string, chanRes chan<- http.Response) {
//получаем урл, отправляем response дальше
}
func parser(chanRes <-chan http.Response, zipChan chan<- Gallery) {
for resp := range chanRes {
//парсим данные, создаем структуру, все как положено и отправляем дальше
zipChan <- gallery
}
}
func zipMaker(zipChan <-chan Gallery) {
for gallery := range zipChan {
//тут качаем картинки из урлов, которые есть в срезе gallery.imgUrls , архивируем
}
}
Answer the question
In order to leave comments, you need to log in
Your program terminates before it has had zipMaker()
time to process all the data: the function main
terminates immediately after the loop terminates.
There are several options for solving this problem, which one to choose depends on the task:
1) if your program should work all the time, then you can simply block its execution. For example, like this:
func main() {
// your code
locker := make(chan struct{})
<-locker
}
httpGet()
in parser()
goroutines either. It's better to return []Gallery
, then iterate over it and call the goroutine for each element. In this case, you need to use sync.WaitGroup
. The code will look something like this:package main
import (
"log"
"sync"
"time"
)
// Для удобства
type Gallery int
func main() {
// То, что должна вернуть функция parser()
results := []Gallery{5, 6, 7, 8}
var wg sync.WaitGroup
for _, r := range results {
wg.Add(1)
go zipMaker(r, &wg)
}
log.Println("Wait")
// Ждём выполнения горутин
wg.Wait()
// "Done" напечатается через 2 секунды после "Wait"
log.Println("Done")
}
func zipMaker(g Gallery, wg *sync.WaitGroup) {
defer wg.Done()
// your code
time.Sleep(time.Second * 2)
}
sync.WaitGroup
allows you to wait for all data to be processed. You can read more here .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question