google / renameio

Package renameio provides a way to atomically create or replace a file or symbolic link.
Apache License 2.0
609 stars 27 forks source link

add a drop-in replacement for ioutil.WriteFile #3

Closed mvdan closed 6 years ago

mvdan commented 6 years ago

Moving from Twitter at https://twitter.com/mvdan_/status/1057289983340544002.

https://golang.org/pkg/io/ioutil/#WriteFile suffices for quite a lot of simple cases where one wants to create or overwrite a file with some contents. That generally is four lines of code, including error handling.

Comparatively, the simplest TempFile example takes nine lines, and requires a defer. This makes it less suitable for these simpler use cases, and makes replacing ioutil.WriteFile with this package harder.

It shouldn't be necessary to strictly stick to ioutil.WriteFile's signature, but one advantage of doing that would be the simplicity of porting code over.

stapelberg commented 6 years ago

This is somewhat related to issue #2 in that maybe we could use the write.File name to be the WriteFile replacement.

In general, I like the idea. I’ll keep notes when switching projects to go-write to confirm where WriteFile would be usable.

mvdan commented 6 years ago

More bikeshedding - if you make write.File be ioutil.WriteFile, then you could have the existing constructor be write.StartFile.

stapelberg commented 6 years ago

I’ve renamed the package to “renameio”. Now, adding a WriteFile method mirroring ioutil.WriteFile seems like a straight-forward thing to do :)