mirror of
https://github.com/SourceFellows/gobuch.git
synced 2025-08-03 05:02:16 +02:00
initial import
This commit is contained in:
31
best-practices/error-handling-2/main.go
Normal file
31
best-practices/error-handling-2/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var err error
|
||||
var f *os.File
|
||||
|
||||
func write(text string) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = fmt.Fprintln(f, text)
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err = os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
write("Hello world 2")
|
||||
write("Hello world 2")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(f)
|
||||
}
|
2
best-practices/error-handling-2/test.txt
Normal file
2
best-practices/error-handling-2/test.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Hello world 2
|
||||
Hello world 2
|
Reference in New Issue
Block a user