Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:21
Show Gist options
  • Select an option

  • Save dacr/e8a1c040573f5bb5783d2cd0e3d98c13 to your computer and use it in GitHub Desktop.

Select an option

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/2e73b5b8cef481b1f3554ea7898d78671c4d9dec
/*?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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// 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