Thanks for continuing the series mate. Very helpful
@yuxiang42188 ай бұрын
Very clear, Thanks
@Awesomekid22833 күн бұрын
You do an awesome job explaining everything! I just wanted to add that it's not a good idea to simply defer closing the file because the `Close` method could return an error. It would be best to use an immediately invoked function with defer where the `Close` method is called, the error is captured, and if it's not `nil`, panic with the error. There are other more graceful things to do for serious projects, but panicking would be enough for this project.
@codeandlearnwithlove2 күн бұрын
Hello, panics are usually avoided in production application. The Close() method on the file returns a error if file is already closed and since in our example, our program finishes there is no related overhead to getting an error from Close(). This is very common pattern through the Go standard lib. Example: cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/os/dir.go;l=123 If you really want to handle the error on the Close() method in a graceful manner, we would create a new function to perform the task and have something like this: ``` defer func() { err = f.Close() }() n, err := writeString("Hello World!!!!", f) if err != nil { panic(err) } fmt.Printf("Written bytes: %d ", n) return err ``` or you can simply return the error: ``` n, err := writeString("Hello World!!!!", f) if err != nil { panic(err) } fmt.Printf("Written bytes: %d ", n) return f.Close() ```
@Awesomekid22832 күн бұрын
@@codeandlearnwithlove thank you for your reply! I guess my IDE simply isn't aware of it because it gave me a warning for ignoring the error. I knew it was for double-close errors (which really are bugs no matter how you look at them) and very very rare disk errors, so I thought there might be some reason to handle it since the method bothers to return something. If it's ignored so often, why does the method even return an error at all? Shouldn't double closes be panics anyway since they're only bugs? Like I feel like double closes are in the same realm as unused local variables, and those are an explicit error in Go
@codeandlearnwithlove2 күн бұрын
@@Awesomekid2283 I think it is a answer for the Go team, but from my experience being explicit and returning a error is a more common pattern across programaming then doing magic in the background. Panics are generally meant to indicate a state that is not expected closing a already closed file is more of a error than a panic.
@AnandKumar-dc2bf2 ай бұрын
superb explanation brother....
@marcelliebreich54308 ай бұрын
thanks. that explained me the concept of io.reader and writer much cleaner!
@Lucidfinder8 ай бұрын
When will you upload topics related to concurrency and channels?
@codeandlearnwithlove8 ай бұрын
We will get there soon. The first part of the course is getting close to the end. The second part will be dedicated to concurrency.