Skip to content

Instantly share code, notes, and snippets.

@Mayankgupta688
Last active February 15, 2023 04:27
Show Gist options
  • Save Mayankgupta688/59c78e89762c4bcaa027670d1a7208e7 to your computer and use it in GitHub Desktop.
Save Mayankgupta688/59c78e89762c4bcaa027670d1a7208e7 to your computer and use it in GitHub Desktop.
cleanupCodeModifiedWithDefer.go
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