rank.go (928B)
1 package main 2 3 func rank(ztchan chan *ZipTemp, ttchan chan []*ZipTemp) { 4 var topTen = make([]*ZipTemp, 10) 5 6 for { 7 zt, more := <-ztchan 8 if !more { 9 close(ttchan) 10 return 11 } 12 13 var newItem bool 14 for i, tt := range topTen { 15 if tt != nil && tt.Temperature > zt.Temperature { 16 continue 17 } 18 19 // We've found a zip code with a higher temperature. 20 // Insert it into the top ten. 21 topTen = append(topTen, nil) 22 copy(topTen[i+1:], topTen[i:]) 23 topTen[i] = zt 24 25 // Drop the last value. 26 topTen = topTen[:10] 27 28 newItem = true 29 break 30 } 31 32 // If the top ten hasn't changed, process the next zip code. 33 if !newItem { 34 continue 35 } 36 37 // If the top ten has changed, message the display goroutine. 38 // Send a copy, to prevent race conditions. 39 var newTopTen = make([]*ZipTemp, 10) 40 41 for i, tt := range topTen { 42 if tt == nil { 43 continue 44 } 45 newTopTen[i] = tt 46 } 47 48 ttchan <- newTopTen 49 } 50 }