本文整理汇总了Golang中io.Reader.profit方法的典型用法代码示例。如果您正苦于以下问题:Golang Reader.profit方法的具体用法?Golang Reader.profit怎么用?Golang Reader.profit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.Reader
的用法示例。
在下文中一共展示了Reader.profit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: process
func process(doneTime time.Time, r io.Reader, a *accounting, resultChan chan result) {
fmt.Println("process")
defer close(resultChan)
tr := newTruckReader(r)
in := make(chan *truck)
out := make(chan *truck)
// Construct the repacker.
newRepacker(in, out)
// A goroutine to read and send trucks
go func() {
defer close(in)
for {
done := time.Now().After(doneTime)
t, err := tr.Next()
if done || err != nil {
if done {
fmt.Println("timeout")
}
if err != nil && err != io.EOF {
fmt.Println("truck reading error: ", err)
}
// Send one more empty truck as a signal that they now
// need to send out any stored boxes.
a.trucksMu.Lock()
a.trucks[idLastTruck] = 0
a.trucksMu.Unlock()
in <- &truck{id: idLastTruck}
return
}
a.boxesMu.Lock()
for _, p := range t.pallets {
for _, b := range p.boxes {
a.boxes[b.canon()] = true
}
}
a.boxesMu.Unlock()
// Remember how many pallets were in the truck.
a.trucksMu.Lock()
a.trucks[t.id] = len(t.pallets)
a.trucksMu.Unlock()
in <- t
}
}()
// Receive the trucks and check them.
for t := range out {
r := result{}
// Only correctly packed pallets count
for pn, p := range t.pallets {
for _, b := range p.boxes {
if !a.boxOk(b) {
log.Printf("box %v in truck %d was not in the input", b.id, t.id)
r.fail = true
}
}
if err := p.IsValid(); err == nil {
r.items += p.Items()
} else {
log.Printf("pallet %v in truck %d is not correctly packed: %v", pn, t.id, err)
r.fail = true
}
}
// Calculate the profit (or loss!) of pallets.
a.trucksMu.Lock()
if _, ok := a.trucks[t.id]; ok {
r.profit = a.trucks[t.id] - len(t.pallets)
} else {
log.Printf("truck %v unknown", t.id)
r.fail = true
}
a.trucksMu.Unlock()
resultChan <- r
}
a.boxesMu.Lock()
if len(a.boxes) != 0 {
log.Printf("%v boxes not seen in the departing trucks", len(a.boxes))
resultChan <- result{fail: true}
}
a.boxesMu.Unlock()
}