must.go 883 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package bio
  5. import (
  6. "io"
  7. "log"
  8. )
  9. // MustClose closes Closer c and calls log.Fatal if it returns a non-nil error.
  10. func MustClose(c io.Closer) {
  11. if err := c.Close(); err != nil {
  12. log.Fatal(err)
  13. }
  14. }
  15. // MustWriter returns a Writer that wraps the provided Writer,
  16. // except that it calls log.Fatal instead of returning a non-nil error.
  17. func MustWriter(w io.Writer) io.Writer {
  18. return mustWriter{w}
  19. }
  20. type mustWriter struct {
  21. w io.Writer
  22. }
  23. func (w mustWriter) Write(b []byte) (int, error) {
  24. n, err := w.w.Write(b)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. return n, nil
  29. }
  30. func (w mustWriter) WriteString(s string) (int, error) {
  31. n, err := io.WriteString(w.w, s)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. return n, nil
  36. }