io/ioutil: predictable suffix for TempFile

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

  1. The caller will have to include the entire extension, including the dot: .bat not bat

    If the caller does not provide an extension will bat just be appended, or are you saying that should be an error?

    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 to TempFileExt(a, b, "")

    I think TempFileTemplate is a bit overly complex for the needs here.

  2. 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.

    ioutil.TempFile("", "foo*.bat")


Comments are closed.