github.com/go4org/go4@v0.0.0-20200104003542-c7e774b10ea0/must/must.go (about)

     1  /*
     2  Copyright 2019 The Perkeep Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package must contains helpers that panic on failure.
    18  package must // import "go4.org/must"
    19  
    20  import "io"
    21  
    22  // Close calls c.Close and panics if it returns an error.
    23  // The panic value is the return value from Close.
    24  func Close(c io.Closer) {
    25  	if err := c.Close(); err != nil {
    26  		panic(err)
    27  	}
    28  }
    29  
    30  // Do runs fn and panics if it returns an error.
    31  // The panic value is the return value from fn.
    32  func Do(fn func() error) {
    33  	if err := fn(); err != nil {
    34  		panic(err)
    35  	}
    36  }