go-util/io/cycler.go

29 lines
619 B
Go

package uio
import "io"
// Cycler stores any io.Closer. When the value is replaced, the old value is
// closed.
type Cycler struct {
value io.Closer
}
// Value returns the value of the Cycler. If there is no value, nil is returned.
func (this *Cycler) Value () io.Closer {
return this.value
}
// Set replaces the value of the Cycler, closing the previous one if it exists.
func (this *Cycler) Set (value io.Closer) error {
err := this.value.Close()
this.value = value
return err
}
// Close closes the Cycler's value early. It will be set to nil.
func (this *Cycler) Close () error {
return this.Set(nil)
}