# @harnyk/chan 中文文档

规范地址：https://npmcn.com/package/%40harnyk/chan/

版本：0\.1\.1
npm 元数据更新时间：2024-07-17T14:39:41.492Z
中文译文更新时间：2026-11-17T14:17:49.808Z
软件包许可：WTFPL

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40harnyk/chan
原文对照：https://npmcn.com/package/%40harnyk/chan/?lang=original
项目主页：https://github.com/harnyk/chan
源码仓库：https://github.com/harnyk/chan

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：58.8 kB，10 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2024-07-07T19:02:51.022Z
- 最新版本发布：2024-07-17T14:39:41.347Z
- 近 30 天下载量：5465（2026-10-18 至 2026-11-16，npm 官方下载统计）
- 已发布版本数：9（近 12 个月 0 个）

## 中文简介

这个包裹是我卑微的尝试 `Chan<T>` typeScript 的类, 尽可能与 Go 相似 `chan T` 类型.

## 安装

```sh
npm install @harnyk/chan
```

也可以使用：`pnpm add @harnyk/chan`、`yarn add @harnyk/chan`、`bun add @harnyk/chan`

## README

# `@harnyk/chan` - 进入类型脚本的频道

这个包裹是我卑微的尝试 `Chan<T>` typeScript 的类, 尽可能与 Go 相似 `chan T` 类型.

## 安装

`npm install @harnyk/chan`

## 使用量

见 [测试](https://npmcn.com/package/%40harnyk/chan/src/chan.test.ts) 财务报告和审定财务报表 [实例](https://npmcn.com/package/%40harnyk/chan/src/examples) 更多信息.

简要参考:

|  | 去等号 | 类型脚本 |
| --- | --- | --- |
| 创建无缓冲的“ 数” 通道 : | 
```go
ch := make(chan int)
```

 | 

```ts
const ch = new Chan<number>();
```

 |
| 创建一个带有固定大小缓冲器的“数字”信道: | 

```go
ch := make(chan int, 5)
```

 | 

```ts
const ch = new Chan<number>(5);
```

 |
| 以\`等待 ' 为理由在\`Chan ' 上标注 | 

```go
for v := range ch {
    fmt.Println(v)
}
```

 | 

```ts
for await (const v of ch) {
    console.log(v);
}
```

 |
| 以“ send” 向“ Chan” 发送一个值 | 

```go
ch <- 42
```

 | 

```ts
await ch.send(42);
```

 |
| 以“ recv” 接收“ Chan” 值 | 

```go
v, ok := <-ch
```

 | 

```ts
const [v, ok] = await ch.recv();
```

 |
| 关闭“ Chan” 语句 | 

```go
close(ch)
```

 | 

```ts
ch.close();
```

 |
| 选择多个频道 | 

```go
select {
    case ch <- 42:
        fmt.Println("sent")
    case v := <-ch1:
        fmt.Printf("Received %d\n", v)
    default:
        fmt.Println("default")
}
```

 | 

```ts
await select()
    .send(ch, 42, () => console.log('sent'))
    .recv(ch1, (v) => console.log(`Received ${v}`))
    .default(() => console.log('default'));
```

 |

## 所支持的内容

-   同步移动 `Chan<T>` 与 `for await`
-   一个同步 `send` 财务报告和审定财务报表 `recv`
-   `select`\-通过多个渠道

## 许可证

WPFPL (英语)

## 贡献者

马克·哈尼克(Mark Harnyk)[https://github.com/harnyk (中文(简体) )](https://github.com/harnyk)(中文(简体) )

## 常见问题

### @harnyk/chan 是什么？

这个包裹是我卑微的尝试 Chan\<T\> typeScript 的类, 尽可能与 Go 相似 chan T 类型\.

### 如何安装 @harnyk/chan？

在项目目录运行 npm install @harnyk/chan，或使用 pnpm add / yarn add / bun add。

### @harnyk/chan 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @harnyk/chan 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @harnyk/chan 可以商用吗？

@harnyk/chan 使用 WTFPL 许可。声明的许可为 WTFPL，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @harnyk/chan 的下载量有多少？

根据 npm 官方下载统计，@harnyk/chan 在 2026/10/18 – 2026/11/16 这 30 天内被下载 5,465 次，日均约 182 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @harnyk/chan 还在维护吗？

最新版本 0\.1\.1 发布于 2024/07/17（2 年前）；至今共发布 9 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/07/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

