philipbohun.com

Glojure by Example: String Formatting

fmt.Sprintf is Go's formatter, available directly. The verbs are the Go ones: %s for a string, %d for an integer, %x for hex, %q for a quoted Go string.

(println (fmt.Sprintf "Hello, %s" "world"))
(println (fmt.Sprintf "%d in hex is %x" 42 42))
(println (fmt.Sprintf "%q" "say \"hi\""))

format is the Clojure wrapper and uses the same family of verbs for simple cases.

(println (format "n=%d" 7))

%q adds Go-style quotes.

$ glj string-formatting.glj
Hello, world
42 in hex is 2a
"say \"hi\""
n=7

Next example: Regular Expressions.