If you don’t control the file descriptor (you don’t open and close it) then flush before exiting.
If you want the written contents displayed, transmitted or saved.
Yes it prints to STDERR.
There are actually more than buffering and the descriptor output; see this video about printing and logging:
Now what’s the “good practice” of doing prints? Using a buffer and flushing every time, or doing it without a buffer? Also, how would I do it without a buffer? I’ve only ever come across the implementation that I showed above.
It does buffer, or did, now it is an implementation detail of Io, all the implementations currently buffer, excluding kqueue which has not implemented debug printing[1] at all.
Yes, that’s best practice. Flush whenever you’ve finished writing and want the user to see what you’ve written. If you’re writing multiple lines at once you only need to flush after the last one.
if you read the source, it uses the global std.Options.debug_io, instead of taking it as a parameter. You are still able to override that, see std.Options for how.
That io is what implements most of the logic behind locking/unlocking stderr.