Closed polderudo closed 2 years ago
Hi @polderudo thanks. I was playing around with this at https://go.dev/play/p/aOS8IOvMHDE and it does seem like the inner function call is executed immediately and passed as input to the outermost function which is executed immediately. Otherwise need to wrap the entire thing inside a defer func() {...}
We will go through the examples and make sure that this is addressed.
@polderudo Where did you find this example? Cannot find anything using defer in this way in our examples repository. Can you specify the source so we can fix it?
Hi @polderudo. It is how defer works, f.Close() will be executed when checkError() evaluated, hence when RenderToPath() called, the file already closed. So the solution is to use deferred anonymous function like this: defer func() { checkError(f.Close()) }()
this way, checkError() will be evaluated later. More about defer can be checked here: https://go.dev/blog/defer-panic-and-recover.
@arknable : Thanks. Working for years now with go but never read about this ... evaluation vs. execution ... Makes sense now. @gunnsth : This is not from your code. I just wanted to get rid of those yellow warnings goland gives you, because you don't handle the return (error) in the defer.
Description
Using a function in the defer for closing the file returned by model.NewPdfReaderFromFile leads to an error in NewImageDevice when calling RenderToPath/Render
Might be a go related problem, but it took me some hours to find the error, thus i'm posting it here.
Here is some demo code to convert a PDF to an image (taken from the examples):
RenderToPath will return an error: 'Image rendering error: invalid content stream object holder (*core.PdfObjectNull)'
Changeing defer checkError(f.Close()) to defer f.Close() solves the issue.