philipbohun.com

Glojure by Example: Multiple Return Values

A Go function that returns several values produces a vector. Bind it with the destructuring from earlier. The last element is often an error. nil means success.

(let [[n err] (strconv.Atoi "42")]
  (println n (nil? err)))

(let [[n err] (strconv.Atoi "nope")]
  (println n (.Error err)))

This is a host convention, not a Clojure language feature. Clojure functions still return one value. When you want to hand back several pieces from Clojure, return a vector or a map on purpose.

(let [[matched err] (regexp.MatchString "^glo" "glojure")]
  (println matched (nil? err)))

A non-nil error is a value. It is not thrown.

$ glj multiple-return-values.glj
42 true
0 strconv.Atoi: parsing "nope": invalid syntax
true true

Next example: Go Pointers.