philipbohun.com

Glojure by Example: Temporary Files and Directories

CreateTemp opens a new file whose name matches a pattern. A * in the pattern is replaced so the name does not collide. Close the file, then RemoveAll the directory that contains it.

(let [[dir err] (os.MkdirTemp "" "glj-temp")
      [file ferr] (os.CreateTemp dir "note-*.txt")]
  (println (nil? err))
  (println (nil? ferr))
  (.Write file "temp")
  (.Close file)
  (println (path:filepath.Ext (.Name file)))
  (println (slurp (.Name file)))
  (os.RemoveAll dir))

The extension comes from the pattern. The directory name itself is random and is not printed.

$ glj temporary-files.glj
true
true
.txt
temp

Next example: Testing.