Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:02
Show Gist options
  • Save dacr/e8a1c040573f5bb5783d2cd0e3d98c13 to your computer and use it in GitHub Desktop.
Save dacr/e8a1c040573f5bb5783d2cd0e3d98c13 to your computer and use it in GitHub Desktop.
go defer / published by https://github.com/dacr/code-examples-manager #e38796ac-819e-496d-b689-27c0236b4e17/8b306935d6d781f14c75fc6b1fa5c6b1ca81f3f1
/*?sr/bin/true; exec /usr/bin/env nix-shell -p go --run "go run $0" #*/
// summary : go defer
// keywords : go, defer, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : e38796ac-819e-496d-b689-27c0236b4e17
// created-on : 2025-03-27T10:06:35+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : nix-shell -p go --run "go run $file"
package main
import (
"fmt"
"os"
)
func writeHello() error {
f, err := os.Create("/tmp/test.txt")
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()
_, err = f.WriteString("Hello World !")
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
func main() {
writeHello()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment