// Handle FX quotes func handleFX(requestFX <-chan string, receiveFX chan<- float64, doneChan <-chan bool) { prices := make(map[string]float64) prices["usd"] = 1 fxChan := make(chan forex.Quote) fxDoneChan := make(chan bool) // Initiate communication and initialize prices map for _, symbol := range currencies { quote := forex.CommunicateFX(symbol, fxChan, fxDoneChan) if quote.Error != nil { log.Fatal(quote.Error) } prices[symbol] = quote.Price } // Handle data until notified of termination for { select { // Incoming forex quote case quote := <-fxChan: if !isError(quote.Error) { prices[quote.Symbol] = quote.Price } // New request for price case symbol := <-requestFX: receiveFX <- prices[symbol] // Termination case <-doneChan: fxDoneChan <- true return } } }
func main() { filename := "btcbook.log" logFile, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { log.Fatal(err) } log.SetOutput(logFile) log.Println("Starting new run") fxChan := make(chan forex.Quote) fxDoneChan := make(chan bool, 1) quote := forex.CommunicateFX("cny", fxChan, fxDoneChan) if quote.Error != nil || quote.Price == 0 { log.Fatal(quote.Error) } cny = quote.Price bookChan := make(chan exchange.Book) if book := btc.CommunicateBook(bookChan); book.Error != nil { log.Fatal(book.Error) } inputChan := make(chan rune) go checkStdin(inputChan) Loop: for { select { case book := <-bookChan: printBook(book) case <-inputChan: btc.Done() fxDoneChan <- true break Loop } } }