Skip to content

Instantly share code, notes, and snippets.

@indrasaputra
Last active October 18, 2018 04:09
Show Gist options
  • Save indrasaputra/0b005dd1a2c292e7b0898a11b2c3966e to your computer and use it in GitHub Desktop.
Save indrasaputra/0b005dd1a2c292e7b0898a11b2c3966e to your computer and use it in GitHub Desktop.
func (c *Checker) CheckStandardsGoroutineChannel(ctx context.Context, svc *Service, svcChan chan *Service, errChan chan []error) {
// this function still does what CheckStandards does,
// but instead of returning the values,
// it sends the values to channel
svcChan <- svc
errChan <- errorList
}
func main() {
ctx := context.Background()
kube := &Kubernetes{}
checker := &Checker{}
services, _ := kube.AllServices(ctx)
// create channel
svcChan := make(chan *Service)
errChan := make(chan []error)
for _, svc := range services {
// spawn goroutine
go checker.CheckStandardsGoroutineChannel(ctx, svc, svcChan, errChan)
}
var result []*Service
var errorLog [][]error
for i := range services {
tmpSvc, tmpErr := <-svcChan, <-errChan
if len(tmpErr) > 0 {
errorLog = append(errorLog, tmpErr)
}
result = append(result, tmpSvc)
}
// do the rest as you like
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment