package sensor import ( "github.com/volker-raschek/flucky/pkg/types" ) func Read(sensors []Sensor, measuredValueType types.MeasuredValueType) ([]*types.MeasuredValue, error) { type result struct { measuredValues []*types.MeasuredValue err error } resultChannel := make(chan *result, len(sensors)) // producers // read measured values for _, s := range sensors { go func(s Sensor) { measuredValues, err := s.Read() resultChannel <- &result{ measuredValues: measuredValues, err: err, } }(s) } // consumer measuredValues := make([]*types.MeasuredValue, 0) counter := len(sensors) for { if counter == 0 { break } select { case result := <-resultChannel: counter-- if result.err != nil { return nil, result.err } measuredValues = append(measuredValues, result.measuredValues...) } } return types.SelectMeasuredValues(measuredValueType, measuredValues), nil }