forked from steger/pr3-ws202526
36 lines
439 B
Go
36 lines
439 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
f := createFile("/tmp/defer.txt")
|
|
writeFile(f)
|
|
closeFile(f)
|
|
}
|
|
|
|
func createFile(p string) *os.File {
|
|
fmt.Println("creating")
|
|
f, err := os.Create(p)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return f
|
|
}
|
|
|
|
func writeFile(f *os.File) {
|
|
fmt.Println("writing")
|
|
fmt.Fprintln(f, "data")
|
|
}
|
|
|
|
func closeFile(f *os.File) {
|
|
fmt.Println("closing")
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|