philipbohun.com

Glojure by Example: Go Methods and Fields

A method call puts the receiver first: (.Replace replacer "foo baz"). A field read uses the same dot with no extra arguments. set! writes a field.

(def replacer (strings.NewReplacer "foo" "bar"))
(println (.Replace replacer "foo baz"))

(def server (new net:http.Server))
(set! (.Addr server) ":9090")
(println (.Addr server))

new allocates the zero value of a struct. Constructor functions such as strings.NewReplacer are plain Go functions and are usually clearer than filling fields by hand. Keyword arguments to new are not supported, so set fields after construction.

(println (nil? server))

The replacer returns a new string. The field read returns what set! stored.

$ glj go-methods.glj
bar baz
:9090
false

Next example: Multiple Return Values.