Created
December 12, 2019 01:32
-
-
Save ronpastore/472f254c8ff6e639c57b6254508803d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package checker | |
import ( | |
"github.com/davecgh/go-spew/spew" | |
"github.com/ronpastore/pricing/pkg/sources" | |
"github.com/ronpastore/pricing/pkg/core" | |
"sync" | |
) | |
func CheckAll() []core.PriceResult { | |
var waitGroup sync.WaitGroup | |
sources := []core.Source{ | |
sources.DummySource{}, | |
sources.AmazonSource{}, | |
} | |
waitGroup.Add(len(sources)) | |
resultChan := make(chan core.PriceResult, len(sources)) | |
for _, entry := range sources { | |
go func(source core.Source) { | |
defer waitGroup.Done() | |
priceResult := source.Check() | |
resultChan <- priceResult | |
}(entry) | |
} | |
waitGroup.Wait() | |
close(resultChan) | |
results := toSlice(resultChan) | |
spew.Dump(results) | |
return results | |
} | |
func toSlice(c chan core.PriceResult) []core.PriceResult { | |
s := make([]core.PriceResult, 0) | |
for i := range c { | |
s = append(s, i) | |
} | |
return s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment