5 ms·
What does this even mean? If I read from a file that doesn't exist, what should the returned object be? Isn't the state of the file directly tied to the error?
by maleldil 2y ago
What does this even mean? If I read from a file that doesn't exist, what should the returned object be? Isn't the state of the file directly tied to the error? If I ignore the error and try to use the file anyway, the following operations wouldn't make sense. How can you read from a non-existent file?
- 9rx 2y ago> If I read from a file that doesn't exist, what should the returned object be? First things first, how can you read from a file that doesn't exist? Even you questioned that later, so this is a strange question. – Do you mean if you try to open a file that doesn't exist? nil would be a reasonable value. It is how Go signifies the absence of something. nil is always useful and with `if file == nil` will tell you that the file isn't there. No need to observe the error. If you need to know why the file isn't there then, sure, you're also going to have to look at the error. Sometimes that is important. In the case of opening a file you realistically will need to know why it failed to open so that you can resolve the condition, but in other cases you don't need to worry about why it failed. Checking the error would be unnecessary in those cases, assuming the function author has some understanding of how to make even half-decent APIs. Let's assume here, for the sake of discussion, that you have no need to know why the file was unable to opened. Again, how do you envision garbage propagating into the rest of your program?
- int_19h 2y agoLet's say you successfully opened a file, but it was on removable media which got ejected mid-read.
- deleted 2y ago[deleted]
- 9rx 2y agoYou’d return -1. A successful read won’t read negative bytes. The non-error value remains useful.
- int_19h 2y agoThat's actually a great example. Note that it is a distinct sentinel error value that needs to be checked for (contrary to your initial assertion), and if you forget to do it, you'll just end up with garbage. But also, let's say the read function in question is named ReadInt32. Then what does it return on error?