This operate is to fetch some xml content material and reserve it. One subject i encounterd is that ship case s.updates ← pending[0]: (pending Merchandise) one by one. the code presently has an error of accessing index out of certain.
right here’s the hyperlink to the complete code : Higher Go Playground
func(s *sub) loop(){
var pending []Merchandise // appended by fetch; consumed by ship
var subsequent time.Time
var err error
var seen = make(map[string]bool) // set of merchandise.GUIDS
for{
// var updates chan Merchandise
var fetchDone chan fetchResult// if non-nil, fetch is working
var fetchDelay time.Length // initally 0
if now := time.Now(); subsequent.After(now) {
fetchDelay = subsequent.Sub(now)
}
var startFetch <-chan time.Time
if fetchDone == nil && len(pending) < maxPending {
startFetch = time.After(fetchDelay)
}
choose{
case errc := <-s.closing:
errc <- err
shut(s.updates)// tells receiver we're accomplished
return
case <-startFetch:
fmt.Println("begin fetch")
var fetched []Merchandise
fetchDone = make(chan fetchResult, 1)
go func(){
fetched, subsequent, err = s.fetcher.Fetch()
fetchDone <- fetchResult{fetched, subsequent, err}
}()
case s.updates <- pending[0]:
fmt.Println("add pending")
if len(pending) > 0 {
pending = pending[1:]
}
case end result := <-fetchDone:
fmt.Println("fetch accomplished")
fetchDone = nil
if end result.err != nil {
subsequent = time.Now().Add(10 * time.Second)
break
}
for _, merchandise := vary end result.fetched {
if !seen[item.Channel.Link] {
pending = append(pending, merchandise)
seen[item.Channel.Title] = true
}
}
}
}
}