philipbohun.com

Glojure by Example: Reading Files

slurp reads a whole file into a string. os.ReadFile is the Go function and returns the bytes plus an error. fmt.Sprintf with %s turns those bytes into a string. print does not add a newline, so the file's own newline ends the line.

(let [[dir _] (os.MkdirTemp "" "glj-read")
      path (path:filepath.Join dir "note.txt")]
  (os.WriteFile path "hello\nworld\n" 0644)
  (print (slurp path))
  (let [[data err] (os.ReadFile path)]
    (println (nil? err))
    (print (fmt.Sprintf "%s" data)))
  (os.RemoveAll dir))

0644 is an octal literal, the usual file mode for a user-writable file. A missing file makes ReadFile return an error value. slurp throws.

Both reads print the same two lines.

$ glj reading-files.glj
hello
world
true
hello
world

Next example: Writing Files.