I'm building a go library that manages and writes data to a file. I have written a file writer that I can pass data to, and it'll write that data to the file via a buffered writer. This means that a service using the library will have to call a close method once the service shuts down to write anything still in the buffer. I see two ways of handling this:
- Expose a
Closemethod that the service can use. - Use channels. If the service closes the channel, that'll close the buffer in the library. My lib function would look like this:
func (r *Repo) Write(ctx context.Context, data <-chan Data, errCh chan<- error) error {
for doc := range data {
err := r.file.Write(ctx, doc)
if err != nil {
errCh <- err
}
}
err = r.file.Close(ctx)
return err
}
My question is if the Write() signature make sense, as it takes a receive channel and a send channel for errors as input? I haven't seen similar examples of this. Maybe there is a better way of organising this?