Skip to content

Commit 4041566

Browse files
myitcvflimzy
authored andcommitted
Further refactor to use defer for close
1 parent 9b28a71 commit 4041566

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

build/build.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -625,20 +625,27 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
625625
if pkg.PkgObj != "" {
626626
fmt.Fprintf(hw, "## %v\n", pkg.ImportPath)
627627

628-
binHash := sha256.New()
629-
binPath, err := os.Executable()
630-
if err != nil {
631-
return nil, fmt.Errorf("could not locate GopherJS binary: %v", err)
632-
}
633-
binFile, err := os.Open(binPath)
634-
if err != nil {
635-
return nil, fmt.Errorf("could not open %v: %v", binPath, err)
628+
hashBin := func() error {
629+
binHash := sha256.New()
630+
binPath, err := os.Executable()
631+
if err != nil {
632+
return fmt.Errorf("could not locate GopherJS binary: %v", err)
633+
}
634+
binFile, err := os.Open(binPath)
635+
if err != nil {
636+
return fmt.Errorf("could not open %v: %v", binPath, err)
637+
}
638+
defer binFile.Close()
639+
if _, err := io.Copy(binHash, binFile); err != nil {
640+
return fmt.Errorf("failed to hash %v: %v", binPath, err)
641+
}
642+
fmt.Fprintf(hw, "gopherjs bin: %#x\n", binHash.Sum(nil))
643+
return nil
636644
}
637-
defer binFile.Close()
638-
if _, err := io.Copy(binHash, binFile); err != nil {
639-
return nil, fmt.Errorf("failed to hash %v: %v", binPath, err)
645+
646+
if err := hashBin(); err != nil {
647+
return nil, err
640648
}
641-
fmt.Fprintf(hw, "gopherjs bin: %#x\n", binHash.Sum(nil))
642649

643650
orderedBuildTags := append([]string{}, s.options.BuildTags...)
644651
sort.Strings(orderedBuildTags)

0 commit comments

Comments
 (0)