nop.go 811 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package pio
  2. import (
  3. "io"
  4. )
  5. // IsNop can check wether an `w` io.Writer
  6. // is a NopOutput.
  7. func IsNop(w io.Writer) bool {
  8. if isN, ok := w.(interface {
  9. IsNop() bool
  10. }); ok {
  11. return isN.IsNop()
  12. }
  13. return false
  14. }
  15. type nopOutput struct{}
  16. func (w *nopOutput) Write(b []byte) (n int, err error) {
  17. // return the actual length in order to `AddPrinter(...)` to be work with io.MultiWriter
  18. return len(b), nil
  19. }
  20. // IsNop defines this wrriter as a nop writer.
  21. func (w *nopOutput) IsNop() bool {
  22. return true
  23. }
  24. // NopOutput returns an `io.Writer` which writes nothing.
  25. func NopOutput() io.Writer {
  26. return &nopOutput{}
  27. }
  28. type nopCloser struct{}
  29. func (c *nopCloser) Close() error {
  30. return nil
  31. }
  32. // NopCloser returns an `io.Closer` which
  33. // does nothing.
  34. func NopCloser() io.Closer {
  35. return &nopCloser{}
  36. }