On Thu, Mar 4, 2021 at 6:19 AM Christian von Kietzell
<[email protected]> wrote:
>
> Hello Gophers,
>
> since I obviously don't have enough Twitter followers to get more than
> one answer to my Go question I'm posting it here ;-)
>
> Would you consider this idiomatic Go code?
>
> https://play.golang.org/p/ymPt_9tKQ9p
>
> I'm talking specifically about returning a cleanup function. My
> reasoning for it goes kinda like this:
>
> - doSomething doesn't care whether the output is a file or a buffer or
> os.Stdout, so it should just get an io.Writer.
> - The cleanup work for using os.Stdout (or a buffer in tests) is
> different than for *os.File.
> - Who's responsible for doing that work? If I return *os.File directly
> from getFile(), there's an implicit assumption that the file needs to be
> closed by the caller. But what if it's os.Stdout?
> - The alternative of putting all that in run() is kinda ugly because
> run() potentially has to do a lot more.
>
> A potential problem I see? The error from os.File.Close() is never
> checked, neither is the error from bufio.Writer.Flush(). In this small
> example that wouldn't bother me because it means the program ends
> anyway. But in other cases it might become more of a problem.

The problems you noted aside, this is similar to context.WithCancel()
that returns a cancel() func. It is a pattern I've seen and used in
the field. In my opinion, it is idiomatic Go code.

>
> What do you think?
>
> Kind regards,
> Chris
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/7baf0ec35696fc5501dfbe6ce8f161f8%40vonkietzell.de.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2RqrruFwSb8EVdtMO%3D9wy2oUu_FekhDyy%3DOkAg%2Bcc%3DK03PQ%40mail.gmail.com.

Reply via email to