github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/readability/err_close_write_good.go (about)

     1  // +build OMIT
     2  
     3  package sample // OMIT
     4  
     5  func run() (err error) {
     6  	in, err := os.Open(*input)
     7  	if err != nil {
     8  		return err
     9  	}
    10  	defer in.Close()
    11  
    12  	out, err := os.Create(*output)
    13  	if err != nil {
    14  		return err
    15  	}
    16  	defer func() { // HL
    17  		if cerr := out.Close(); err == nil { // HL
    18  			err = cerr // HL
    19  		} // HL
    20  	}() // HL
    21  	// some code
    22  }