Right now ioutil.TempFile creates files with a given prefix as the basename. This is fine for the usual /tmp usage, where all the files in that directory are temporary files. However, for e.g. replacing a state file ~/.foo.json with a new one, what I'd want is writing to e.g. ~/.foo.<RANDOMNESS>.tmp and renaming that over ~/.foo.json. This way I can make e.g. backup and editor software ignore *.tmp files, and even clean old leftover temp files automatically.
3 thoughts on “io/ioutil: predictable suffix for TempFile”
Comments are closed.
I just mean that the the filename is made of
prefix + random + extension
, therefore callers will usually call TempFile() with a suffix that includes the “.” if they want a “.”.Callers might assume the formula is
prefix + random + "." + extension
and they would be wrong.TempFile(a, b)
is simply a call toTempFileExt(a, b, "")
I think TempFileTemplate is a bit overly complex for the needs here.
There’s ioutil.TempDir already, that’s one option if you want control over file suffixes.
We don’t yet know how we’d want to add support for suffixes, or if we really do. One option instead of TempFile2 is to define that the current prefix is a pattern so that if there’s a * that’s where the randomness goes.
etc.
@felipeek It should be in Go1.11beta1 already. You can try it out!
https://groups.google.com/forum/#!topic/golang-announce/m1Szl8Mu_Fs
Also, closed issues aren’t monitored. If you find a bug please open a new issue.