Namespaces

When writing real-world applications we need the tools to organize our code into logical groups. In object-oriented languages it’s common to use classes and define methods as members of a class. In Clojure, we group our functions into namespaces instead. Let’s look at how a namespace is defined.

 
(​ns​ myns)
 
 
(​defn​ print-message [message]
 
(​println​ ​"message:"​ message))
 
 
(​defn​ say-hello [user]
 
(print-message (​str​ ​"hello "​ user))

Here we have a namespace called myns containing two functions, print-message and say-hello. The functions in the same namespace can call each other directly. However, if we wanted to call these functions from a different namespace we would have to reference the myns first in the declaration ...

Get Web Development with Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.