O'Reilly logo

Web Development with Clojure by Dmitri Sotnikov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required