philipbohun.com

Glojure by Example: Go Pointers

Clojure locals are not boxes, and Glojure has no & operator. When a Go API wants a pointer, go/new allocates a zero value and returns a pointer to it. go/deref reads through the pointer.

(def box (go/new go/int))
(println (go/deref box))

Some constructors hand back a pointer because the Go API updates it later. flag.String does that. Inside glj, build a FlagSet instead of using the global parser, or the parser will also see glj's own arguments.

(def flags (flag.NewFlagSet "example" flag.ContinueOnError))
(def name (.String flags "name" "world" "who to greet"))
(println (string? name))
(println (go/deref name))

For Clojure state, prefer an atom. Pointers are for talking to Go.

go/new of go/int is the zero integer. The flag still holds its default.

$ glj go-pointers.glj
0
false
world

Next example: Go Slices.