0Pricing
Clojure Functional Programming & JVM Backend Development · 课时

core.async 通道与 Go 代码块

除了引用和原子,core.async 还为 Clojure 带来了 CSP 风格的并发。本课程将教您使用通道和 Go 代码块,并通过通信进程协调异步工作。

core.async 通道与 Go 代码块 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why core.async

Atoms and refs manage shared state. core.async manages communication between independent processes using channels, inspired by CSP.

Requiring the Library

Pull in the core functions you will use.

(require '[clojure.core.async :as a
           :refer [chan go >! <! <!! >!!]])

Creating a Channel

A channel is a queue that processes use to hand off values. Create one with chan.

(def c (chan))
; an unbuffered channel

Go Blocks

A go block runs its body asynchronously on a thread pool, parking instead of blocking at channel operations.

(go (println "running asynchronously"))

Putting and Taking

Inside a go block, >! puts a value and <! takes one. Both park until the other side is ready.

(let [c (chan)]
  (go (>! c 42))
  (go (println "got" (<! c))))

Blocking Variants

Outside go blocks, use the double-bang forms >!! and <!!, which block the real thread. Useful at the REPL or in main.

(let [c (chan)]
  (go (>! c "hello"))
  (println (<!! c)))

Buffered Channels

Give a channel a buffer so puts do not park until the buffer is full. This decouples fast producers from slower consumers.

(def c (chan 10))
; up to 10 values can sit in the buffer

Selecting With alts!

alts! waits on several channels at once and proceeds with whichever is ready first, enabling timeouts and racing.

(go
  (let [[v ch] (a/alts! [c (a/timeout 1000)])]
    (println "got" v "from" ch)))

Closing Channels

Closing a channel signals no more values. Takes then return nil, which consumers use as a stop signal.

(a/close! c)
; subsequent <! returns nil

Pipelines of Processes

Wire go blocks together with channels to form a pipeline: each stage takes from one channel, transforms, and puts onto the next. State stays local to each process.

Channels vs Atoms

Use atoms/refs for shared mutable state; use channels to coordinate the flow of work between processes. They solve different problems and often combine.

Quick Check

Test your core.async knowledge.

Recap

You learned channels, go blocks, parking puts/takes, blocking variants, buffers, alts! for selection and timeouts, closing channels, and how channels complement atoms and refs.

常见问题解答

「core.async 通道与 Go 代码块」课时是免费的吗?

是的 — 「core.async 通道与 Go 代码块」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

「core.async 通道与 Go 代码块」这节课中我会学到什么?

除了引用和原子,core.async 还为 Clojure 带来了 CSP 风格的并发。本课程将教您使用通道和 Go 代码块,并通过通信进程协调异步工作。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「core.async 通道与 Go 代码块」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?

能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 用于状态管理的引用、代理与原子
  2. 软件事务内存(STM)
  3. 承诺、未来与异步操作
  4. core.async 通道与 Go 代码块
← 返回 Clojure Functional Programming & JVM Backend Development