Last active
February 15, 2023 04:27
-
-
Save Mayankgupta688/59c78e89762c4bcaa027670d1a7208e7 to your computer and use it in GitHub Desktop.
cleanupCodeModifiedWithDefer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nupResources.go | |
package main | |
import ( | |
"io" | |
"log" | |
"os" | |
) | |
func main() { | |
if err := write("readme.txt", "This is a readme file"); err != nil { | |
log.Fatal("failed to write file:", err) | |
} | |
} | |
func write(fileName string, text string) error { | |
file, err := os.Create(fileName) | |
defer file.Close() | |
if err != nil { | |
return err | |
} | |
_, err = io.WriteString(file, text) | |
if err != nil { | |
return err | |
} | |
file.Close() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment