must.go (883B)
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 5 package bio 6 7 import ( 8 "io" 9 "log" 10 ) 11 12 // MustClose closes Closer c and calls log.Fatal if it returns a non-nil error. 13 func MustClose(c io.Closer) { 14 if err := c.Close(); err != nil { 15 log.Fatal(err) 16 } 17 } 18 19 // MustWriter returns a Writer that wraps the provided Writer, 20 // except that it calls log.Fatal instead of returning a non-nil error. 21 func MustWriter(w io.Writer) io.Writer { 22 return mustWriter{w} 23 } 24 25 type mustWriter struct { 26 w io.Writer 27 } 28 29 func (w mustWriter) Write(b []byte) (int, error) { 30 n, err := w.w.Write(b) 31 if err != nil { 32 log.Fatal(err) 33 } 34 return n, nil 35 } 36 37 func (w mustWriter) WriteString(s string) (int, error) { 38 n, err := io.WriteString(w.w, s) 39 if err != nil { 40 log.Fatal(err) 41 } 42 return n, nil 43 }