(ns myapp.core
(:require
[clojure.core.async :refer [>! <! >!! <!! chan go-loop]])
(:gen-class))
(def this-ns *ns*)
(defn get-ns [c0]
(go-loop []
(let [x (<! c0)]
(println (str "this ns -> " this-ns))
(println (str "go block ns -> " *ns*)))
(recur)))
(defn -main [& args]
(let [c0 (chan)]
(get-ns c0)
(>!! c0 1)))
输出:
this ns -> myapp.core
go block ns -> user
任何人都可以解释为什么命名空间在go块内是不同的吗?
答案 0 :(得分:6)
我不喜欢&#39;认为这与core.async
有关。它只是*ns*
的工作原理 - 它包含当前的repls当前命名空间
clojure.core/*ns*
A clojure.lang.Namespace object representing the current namespace.
换句话说,*ns*
依赖于您从调用的命名空间
示例:
; <current-ns>=> <code>
user=> (load-file "yourcode.clj")
#'myapp.core/-main
user=> (myapp.core/-main)
this ns -> myapp.core
go block ns -> user
true
user=> (in-ns 'myapp.core) ; <-- switching namespace
#object[clojure.lang.Namespace 0x679e26fe "myapp.core"]
myapp.core=> (-main)
this ns -> myapp.core
go block ns -> myapp.core
true