philipbohun.com

Glojure by Example: Go Slices

Go slices are host values, distinct from Clojure vectors. go/make builds one. go/slice-of is the slice type constructor. go/append returns the grown slice, just as append does in Go, so rebind the name.

(def langs (go/make (go/slice-of go/string) 0 2))
(def langs (go/append langs "clojure" "go"))
(println (go/len langs))
(println (go/index langs 0))
(println (vec langs))

vec copies a slice into a vector. Many Go functions that want a []byte accept a Clojure string, because Glojure coerces a string to bytes for a byte-slice parameter. File writing below relies on that.

(println (go/len (go/append langs "lisp")))

Index 0 is the first element. vec prints the slice as a vector.

$ glj go-slices.glj
2
clojure
[clojure go]
3

Next example: Goroutines.