atomicerror.go 376 B

1234567891011121314151617181920212223
  1. package errorx
  2. import "sync/atomic"
  3. // AtomicError defines an atomic error.
  4. type AtomicError struct {
  5. err atomic.Value // error
  6. }
  7. // Set sets the error.
  8. func (ae *AtomicError) Set(err error) {
  9. if err != nil {
  10. ae.err.Store(err)
  11. }
  12. }
  13. // Load returns the error.
  14. func (ae *AtomicError) Load() error {
  15. if v := ae.err.Load(); v != nil {
  16. return v.(error)
  17. }
  18. return nil
  19. }