philipbohun.com

Glojure by Example: Writing Files

os.WriteFile creates or replaces a file. Pass a string where Go wants []byte. Glojure coerces it. A second write with the same path replaces the contents.

(let [[dir _] (os.MkdirTemp "" "glj-write")
      path (path:filepath.Join dir "out.txt")]
  (os.WriteFile path "one\ntwo\n" 0644)
  (print (slurp path))
  (os.WriteFile path "replaced\n" 0644)
  (print (slurp path))
  (os.RemoveAll dir))

The examples use WriteFile and slurp because those work in current glj. Temporary directories from MkdirTemp are removed before the program exits.

The second write replaces the first.

$ glj writing-files.glj
one
two
replaced

Next example: Line Filters.