Last active
February 3, 2026 20:21
-
-
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
This file contains hidden or 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
| /*?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