Understanding io.Reader & io.Writer in Go with Code Examples

  Рет қаралды 2,458

Code & Learn

Code & Learn

Күн бұрын

Пікірлер: 12
@sarmhn123
@sarmhn123 8 ай бұрын
Thanks for continuing the series mate. Very helpful
@yuxiang4218
@yuxiang4218 8 ай бұрын
Very clear, Thanks
@Awesomekid2283
@Awesomekid2283 3 күн бұрын
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.
@codeandlearnwithlove
@codeandlearnwithlove 2 күн бұрын
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() ```
@Awesomekid2283
@Awesomekid2283 2 күн бұрын
@@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
@codeandlearnwithlove
@codeandlearnwithlove 2 күн бұрын
@@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-dc2bf
@AnandKumar-dc2bf 2 ай бұрын
superb explanation brother....
@marcelliebreich5430
@marcelliebreich5430 8 ай бұрын
thanks. that explained me the concept of io.reader and writer much cleaner!
@Lucidfinder
@Lucidfinder 8 ай бұрын
When will you upload topics related to concurrency and channels?
@codeandlearnwithlove
@codeandlearnwithlove 8 ай бұрын
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.
@CASnumber
@CASnumber 8 ай бұрын
Great video man. Thanks
@portiseremacunix
@portiseremacunix 7 ай бұрын
Thanks! very clear!
Advanced Golang: Generics Explained
13:37
Code With Ryan
Рет қаралды 68 М.
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 16 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 7 МЛН
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
Building the Ultimate Workout Tracker with React Native & MongoDB
3:59:34
Master Golang with Polymorphism
18:17
Kantan Coding
Рет қаралды 4,7 М.
My 2 Year Journey of Learning C, in 9 minutes
8:42
VoxelRifts
Рет қаралды 650 М.
Go (Golang) io.Reader Interface
27:48
Golang Cafe
Рет қаралды 14 М.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Anthony GG
Рет қаралды 25 М.
Understanding Channels in Go with Code Examples
13:03
Code & Learn
Рет қаралды 362
This is why Go Channels are awesome
6:06
Web Dev Cody
Рет қаралды 15 М.
Обработка ошибок в #Golang, до и после v1.20
21:29
Golang: The Last Interface Explanation You'll Ever Need
17:58
Flo Woelki
Рет қаралды 20 М.
Haunted House 😰😨 LeoNata family #shorts
00:37
LeoNata Family
Рет қаралды 16 МЛН