Clojure China

Put! 和 >! 的差别在哪里啊?

#1

网上演示 ClojureScript 的代码, 提到 >! 不能解决的问题, 用 put! 就解决了,
我本来还以为两者是同一个函数的不同写法, 看起来是有区别的?

(go
  (let [c (chan)]
    (>! c "hello")
    (.write js/document (<! c))
    (close! c)))
;; put! version
(go
  (let [c (chan)]
    (put! c "hello")
    (.write js/document (<! c))
    (close! c)))
#2

put! 是异步的就是这个区别?

我觉得这个问题的关键不在 >!put! ,而在于 (chan) 干了什么事情?

(chan) 建立了一个没有 buffer 的通道, 没有 buffer 意味着什么? 意味着必须又接收者 (<! c) 才能投递过去。

(chan 1) 就没有这个问题。

#3

不理解, 上边的代码除了这个函数调用其他都是一样的啊…

#4

上面那个用 <! 的暂停了 go 块,需要等待一个 >! 来打破暂停,但是那个操作因为 go 块暂停永远执行不了了。put! 不是暂停操作,自然没问题。