philipbohun.com

Glojure by Example: Go Packages

Exported Go names are available as Clojure vars. A slash in a Go import path is written as a colon, because / already separates a Clojure namespace from a name. net/http is net:http. The name after the dot is the exported Go identifier.

(println (fmt.Sprintf "Hello, %s" "world"))
(println (strings.ToUpper "glojure"))
(println (strings.Contains "glojure" "loj"))
(println (math.Sqrt 9))
(println net:http.MethodGet)
(println net:http.MethodPost)

glj exports a fixed set of standard-library packages, including fmt, strings, strconv, math, math/rand, os, os/exec, os/signal, io, bytes, path/filepath, regexp, time, sync, sync/atomic, context, flag, net/http, reflect, sort, unicode, errors, and runtime. Packages outside that set, such as bufio, encoding/json, and log, need a generated package map compiled into a custom program. The examples on this site stay inside what glj provides.

Package functions are ordinary Clojure calls.

$ glj go-packages.glj
Hello, world
GLOJURE
true
3.0
GET
POST

Next example: Go Methods and Fields.