mirror of
				https://github.com/SourceFellows/gobuch.git
				synced 2025-11-04 07:36:19 +01:00 
			
		
		
		
	initial import
This commit is contained in:
		
							
								
								
									
										48
									
								
								best-practices/error-handling-3/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								best-practices/error-handling-3/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func NewWriter(w io.Writer) *writer {
 | 
			
		||||
	return &writer{w, nil}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type writer struct {
 | 
			
		||||
	w   io.Writer
 | 
			
		||||
	err error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *writer) write(text string) {
 | 
			
		||||
	if w.err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	_, w.err = fmt.Fprintln(w.w, text)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *writer) Err() error {
 | 
			
		||||
	return w.err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	f, err := os.OpenFile("test.txt", os.O_WRONLY, os.ModePerm)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
 | 
			
		||||
	w := NewWriter(f)
 | 
			
		||||
 | 
			
		||||
	w.write("Hello world 3")
 | 
			
		||||
	w.write("Hello world 3")
 | 
			
		||||
 | 
			
		||||
	if err = w.Err(); err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(f)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								best-practices/error-handling-3/test.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								best-practices/error-handling-3/test.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
Hello world 3
 | 
			
		||||
Hello world 3
 | 
			
		||||
		Reference in New Issue
	
	Block a user