import{_ as l,o as p,c as o,k as s,a,X as n}from"./chunks/framework.6e839c56.js";const b=JSON.parse('{"title":"Concurrency","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/concurrency.md","filePath":"en/api/packages/concurrency.md"}'),e={name:"en/api/packages/concurrency.md"},c=s("h1",{id:"Concurrency",tabindex:"-1"},[a("Concurrency "),s("a",{class:"header-anchor",href:"#Concurrency","aria-label":'Permalink to "Concurrency"'},"​")],-1),t=s("p",null,"Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel.",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"Source-",tabindex:"-1"},[a("Source: "),s("a",{class:"header-anchor",href:"#Source-","aria-label":'Permalink to "Source:"'},"​")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/concurrency/channel.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/concurrency/channel.go")])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),A=n(`<h2 id="Usage-" tabindex="-1">Usage: <a class="header-anchor" href="#Usage-" aria-label="Permalink to &quot;Usage:&quot;">​</a></h2><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span></code></pre></div>`,2),u=s("div",{STYLE:"page-break-after: always;"},null,-1),E=n('<h2 id="Index" tabindex="-1">Index <a class="header-anchor" href="#Index" aria-label="Permalink to &quot;Index&quot;">​</a></h2><h3 id="Channel" tabindex="-1">Channel <a class="header-anchor" href="#Channel" aria-label="Permalink to &quot;Channel&quot;">​</a></h3><ul><li><a href="#NewChannel">NewChannel</a></li><li><a href="#Bridge">Bridge</a></li><li><a href="#FanIn">FanIn</a></li><li><a href="#Generate">Generate</a></li><li><a href="#Or">Or</a></li><li><a href="#OrDone">OrDone</a></li><li><a href="#Repeat">Repeat</a></li><li><a href="#RepeatFn">RepeatFn</a></li><li><a href="#Take">Take</a></li><li><a href="#Tee">Tee</a></li></ul>',3),h=s("div",{STYLE:"page-break-after: always;"},null,-1),d=n(`<link rel="stylesheet" type="text/css" href="/styles/api_doc.css"><h2 id="Documentation" tabindex="-1">Documentation <a class="header-anchor" href="#Documentation" aria-label="Permalink to &quot;Documentation&quot;">​</a></h2><h2 id="Channel-1" tabindex="-1">Channel <a class="header-anchor" href="#Channel-1" aria-label="Permalink to &quot;Channel&quot;">​</a></h2><h3 id="NewChannel" tabindex="-1"><span id="NewChannel">NewChannel</span> <a class="header-anchor" href="#NewChannel" aria-label="Permalink to &quot;&lt;span id=&quot;NewChannel&quot;&gt;NewChannel&lt;/span&gt;&quot;">​</a></h3><p>Create a Channel pointer instance.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">Channel</span><span style="color:#F6F6F4;">[T any] </span><span style="color:#F286C4;">struct</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> NewChannel[T any]() </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">]</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> NewChannel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">]()</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">]</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/7aB4KyMMp9A" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Bridge" tabindex="-1"><span id="Bridge">Bridge</span> <a class="header-anchor" href="#Bridge" aria-label="Permalink to &quot;&lt;span id=&quot;Bridge&quot;&gt;Bridge&lt;/span&gt;&quot;">​</a></h3><p>Link multiple channels into one channel until cancel the context.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Bridge</span><span style="color:#F6F6F4;">(ctx context.Context, chanStream </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">chan</span><span style="color:#EE6666;font-style:italic;text-decoration:underline;"> </span><span style="color:#F6F6F4;">&lt;-</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Bridge</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> chanStream </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">chan</span><span style="color:#B31D28;font-style:italic;"> </span><span style="color:#393A34;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/qmWSy1NVF-Y" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    genVals </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">() </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">chan</span><span style="color:#EE6666;font-style:italic;text-decoration:underline;"> </span><span style="color:#F6F6F4;">&lt;-</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        out </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">make</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> (</span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">))</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">close</span><span style="color:#F6F6F4;">(out)</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">                stream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">make</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">                stream </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> i</span></span>
<span class="line"><span style="color:#F6F6F4;">                </span><span style="color:#97E1F1;">close</span><span style="color:#F6F6F4;">(stream)</span></span>
<span class="line"><span style="color:#F6F6F4;">                out </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> stream</span></span>
<span class="line"><span style="color:#F6F6F4;">            }</span></span>
<span class="line"><span style="color:#F6F6F4;">        }()</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> out</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Bridge</span><span style="color:#F6F6F4;">(ctx, </span><span style="color:#97E1F1;">genVals</span><span style="color:#F6F6F4;">()) {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 5</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">genVals</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">chan</span><span style="color:#B31D28;font-style:italic;"> </span><span style="color:#393A34;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B07D48;">out</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">make</span><span style="color:#999999;">(</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">close</span><span style="color:#999999;">(</span><span style="color:#393A34;">out</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">                </span><span style="color:#B07D48;">stream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">make</span><span style="color:#999999;">(</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">                stream </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> i</span></span>
<span class="line"><span style="color:#393A34;">                </span><span style="color:#998418;">close</span><span style="color:#999999;">(</span><span style="color:#393A34;">stream</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">                out </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> stream</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> out</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Bridge</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#998418;">genVals</span><span style="color:#999999;">())</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 5</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="FanIn" tabindex="-1"><span id="FanIn">FanIn</span> <a class="header-anchor" href="#FanIn" aria-label="Permalink to &quot;&lt;span id=&quot;FanIn&quot;&gt;FanIn&lt;/span&gt;&quot;">​</a></h3><p>Merge multiple channels into one channel until cancel the context.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">FanIn</span><span style="color:#F6F6F4;">(ctx context.Context, channels </span><span style="color:#F286C4;">...&lt;-chan</span><span style="color:#F6F6F4;"> T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">FanIn</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> channels </span><span style="color:#AB5959;">...&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/2VYFMexEvTm" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    channels </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">make</span><span style="color:#F6F6F4;">([]</span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        channels[i] </span><span style="color:#F286C4;">=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, c.</span><span style="color:#97E1F1;">Repeat</span><span style="color:#F6F6F4;">(ctx, i), </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    chs </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">FanIn</span><span style="color:#F6F6F4;">(ctx, channels</span><span style="color:#F286C4;">...</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> chs {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v) </span><span style="color:#7B7F8B;">//1 1 0 0 or 0 0 1 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">channels</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">make</span><span style="color:#999999;">([]</span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        channels</span><span style="color:#999999;">[</span><span style="color:#393A34;">i</span><span style="color:#999999;">]</span><span style="color:#393A34;"> </span><span style="color:#999999;">=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Repeat</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> i</span><span style="color:#999999;">),</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">chs</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">FanIn</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> channels</span><span style="color:#AB5959;">...</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> chs </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//1 1 0 0 or 0 0 1 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Repeat" tabindex="-1"><span id="Repeat">Repeat</span> <a class="header-anchor" href="#Repeat" aria-label="Permalink to &quot;&lt;span id=&quot;Repeat&quot;&gt;Repeat&lt;/span&gt;&quot;">​</a></h3><p>Create channel, put values into the channel repeatly until cancel the context.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/k5N_ALVmYjE" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Repeat</span><span style="color:#F6F6F4;">(ctx context.Context, values </span><span style="color:#F286C4;">...</span><span style="color:#F6F6F4;">T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Repeat</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> values </span><span style="color:#AB5959;">...</span><span style="color:#393A34;">T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, c.</span><span style="color:#97E1F1;">Repeat</span><span style="color:#F6F6F4;">(ctx, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">), </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> intStream {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Repeat</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">),</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> intStream </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Generate" tabindex="-1"><span id="Generate">Generate</span> <a class="header-anchor" href="#Generate" aria-label="Permalink to &quot;&lt;span id=&quot;Generate&quot;&gt;Generate&lt;/span&gt;&quot;">​</a></h3><p>Creates a channel, then put values into the channel.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Generate</span><span style="color:#F6F6F4;">(ctx context.Context, values </span><span style="color:#F286C4;">...</span><span style="color:#F6F6F4;">T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Generate</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> values </span><span style="color:#AB5959;">...</span><span style="color:#393A34;">T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/7aB4KyMMp9A" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Generate</span><span style="color:#F6F6F4;">(ctx, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">intStream)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">intStream)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">intStream)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Generate</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">intStream</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">intStream</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">intStream</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="RepeatFn" tabindex="-1"><span id="RepeatFn">RepeatFn</span> <a class="header-anchor" href="#RepeatFn" aria-label="Permalink to &quot;&lt;span id=&quot;RepeatFn&quot;&gt;RepeatFn&lt;/span&gt;&quot;">​</a></h3><p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/4J1zAWttP85" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">RepeatFn</span><span style="color:#F6F6F4;">(ctx context.Context, fn </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">() T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">RepeatFn</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> fn </span><span style="color:#1E754F;">func</span><span style="color:#999999;">()</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fn </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">() </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">hello</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, c.</span><span style="color:#97E1F1;">RepeatFn</span><span style="color:#F6F6F4;">(ctx, fn), </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> intStream {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// hello</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// hello</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// hello</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">fn</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">string</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">hello</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">RepeatFn</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> fn</span><span style="color:#999999;">),</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> intStream </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// hello</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// hello</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// hello</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Or" tabindex="-1"><span id="Or">Or</span> <a class="header-anchor" href="#Or" aria-label="Permalink to &quot;&lt;span id=&quot;Or&quot;&gt;Or&lt;/span&gt;&quot;">​</a></h3><p>Read one or more channels into one channel, will close when any readin channel is closed.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/Wqz9rwioPww" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Or</span><span style="color:#F6F6F4;">(channels </span><span style="color:#F286C4;">...&lt;-chan</span><span style="color:#F6F6F4;"> T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Or</span><span style="color:#999999;">(</span><span style="color:#393A34;">channels </span><span style="color:#AB5959;">...&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    sig </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(after time.Duration) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> any {</span></span>
<span class="line"><span style="color:#F6F6F4;">        c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">make</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> any)</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">close</span><span style="color:#F6F6F4;">(c)</span></span>
<span class="line"><span style="color:#F6F6F4;">            time.</span><span style="color:#97E1F1;">Sleep</span><span style="color:#F6F6F4;">(after)</span></span>
<span class="line"><span style="color:#F6F6F4;">        }()</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> c</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    start </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> time.</span><span style="color:#97E1F1;">Now</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[any]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">c.</span><span style="color:#97E1F1;">Or</span><span style="color:#F6F6F4;">(</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#97E1F1;">sig</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">1</span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">time.Second),</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#97E1F1;">sig</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">2</span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">time.Second),</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#97E1F1;">sig</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">3</span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">time.Second),</span></span>
<span class="line"><span style="color:#F6F6F4;">    )</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">done after </span><span style="color:#BF9EEE;">%v</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, time.</span><span style="color:#97E1F1;">Since</span><span style="color:#F6F6F4;">(start)) </span><span style="color:#7B7F8B;">//1.003s</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">sig</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">after time</span><span style="color:#999999;">.</span><span style="color:#393A34;">Duration</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> any </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">make</span><span style="color:#999999;">(</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> any</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">close</span><span style="color:#999999;">(</span><span style="color:#393A34;">c</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            time</span><span style="color:#999999;">.</span><span style="color:#998418;">Sleep</span><span style="color:#999999;">(</span><span style="color:#393A34;">after</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> c</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">start</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> time</span><span style="color:#999999;">.</span><span style="color:#998418;">Now</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#393A34;">any</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">c</span><span style="color:#999999;">.</span><span style="color:#998418;">Or</span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#998418;">sig</span><span style="color:#999999;">(</span><span style="color:#2F798A;">1</span><span style="color:#AB5959;">*</span><span style="color:#393A34;">time</span><span style="color:#999999;">.</span><span style="color:#393A34;">Second</span><span style="color:#999999;">),</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#998418;">sig</span><span style="color:#999999;">(</span><span style="color:#2F798A;">2</span><span style="color:#AB5959;">*</span><span style="color:#393A34;">time</span><span style="color:#999999;">.</span><span style="color:#393A34;">Second</span><span style="color:#999999;">),</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#998418;">sig</span><span style="color:#999999;">(</span><span style="color:#2F798A;">3</span><span style="color:#AB5959;">*</span><span style="color:#393A34;">time</span><span style="color:#999999;">.</span><span style="color:#393A34;">Second</span><span style="color:#999999;">),</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">done after </span><span style="color:#A65E2B;">%v</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> time</span><span style="color:#999999;">.</span><span style="color:#998418;">Since</span><span style="color:#999999;">(</span><span style="color:#393A34;">start</span><span style="color:#999999;">))</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//1.003s</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="OrDone" tabindex="-1"><span id="OrDone">OrDone</span> <a class="header-anchor" href="#OrDone" aria-label="Permalink to &quot;&lt;span id=&quot;OrDone&quot;&gt;OrDone&lt;/span&gt;&quot;">​</a></h3><p>Read a channel into another channel, will close until cancel context.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/lm_GoS6aDjo" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">OrDone</span><span style="color:#F6F6F4;">(ctx context.Context, channel </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">OrDone</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> channel </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, c.</span><span style="color:#97E1F1;">Repeat</span><span style="color:#F6F6F4;">(ctx, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">), </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">OrDone</span><span style="color:#F6F6F4;">(ctx, intStream) {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Repeat</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">),</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">OrDone</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> intStream</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Take" tabindex="-1"><span id="Take">Take</span> <a class="header-anchor" href="#Take" aria-label="Permalink to &quot;&lt;span id=&quot;Take&quot;&gt;Take&lt;/span&gt;&quot;">​</a></h3><p>Create a channel whose values are taken from another channel with limit number.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/9Utt-1pDr2J" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Take</span><span style="color:#F6F6F4;">(ctx context.Context, valueStream </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T, number </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> valueStream </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> number </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">make</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">chan</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">2</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">3</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">4</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">close</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, numbers, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> intStream {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">make</span><span style="color:#999999;">(</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    numbers </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    numbers </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span></span>
<span class="line"><span style="color:#393A34;">    numbers </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span></span>
<span class="line"><span style="color:#393A34;">    numbers </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span></span>
<span class="line"><span style="color:#393A34;">    numbers </span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">close</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> intStream </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Tee" tabindex="-1"><span id="Tee">Tee</span> <a class="header-anchor" href="#Tee" aria-label="Permalink to &quot;&lt;span id=&quot;Tee&quot;&gt;Tee&lt;/span&gt;&quot;">​</a></h3><p>Split one chanel into two channels, until cancel the context.</p><p><b>Signature: <span class="run-container"><a href="https://go.dev/play/p/3TQPKnCirrP" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">Channel[T]) </span><span style="color:#62E884;">Tee</span><span style="color:#F6F6F4;">(ctx context.Context, in </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T) (</span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T, </span><span style="color:#F286C4;">&lt;-chan</span><span style="color:#F6F6F4;"> T)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">Channel</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Tee</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx context</span><span style="color:#999999;">.</span><span style="color:#393A34;">Context</span><span style="color:#999999;">,</span><span style="color:#393A34;"> in </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&lt;-</span><span style="color:#1E754F;">chan</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">context</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    ctx, cancel </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> context.</span><span style="color:#97E1F1;">WithCancel</span><span style="color:#F6F6F4;">(context.</span><span style="color:#97E1F1;">Background</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">defer</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">cancel</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    c </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> concurrency.NewChannel[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]()</span></span>
<span class="line"><span style="color:#F6F6F4;">    intStream </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Take</span><span style="color:#F6F6F4;">(ctx, c.</span><span style="color:#97E1F1;">Repeat</span><span style="color:#F6F6F4;">(ctx, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">), </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    ch1, ch2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> c.</span><span style="color:#97E1F1;">Tee</span><span style="color:#F6F6F4;">(ctx, intStream)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">range</span><span style="color:#F6F6F4;"> ch1 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">&lt;-</span><span style="color:#F6F6F4;">ch2)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">context</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/concurrency</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">cancel</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> context</span><span style="color:#999999;">.</span><span style="color:#998418;">WithCancel</span><span style="color:#999999;">(</span><span style="color:#393A34;">context</span><span style="color:#999999;">.</span><span style="color:#998418;">Background</span><span style="color:#999999;">())</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">defer</span><span style="color:#393A34;"> </span><span style="color:#998418;">cancel</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">c</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> concurrency</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewChannel</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]()</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">intStream</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Take</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Repeat</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">),</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ch1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ch2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> c</span><span style="color:#999999;">.</span><span style="color:#998418;">Tee</span><span style="color:#999999;">(</span><span style="color:#393A34;">ctx</span><span style="color:#999999;">,</span><span style="color:#393A34;"> intStream</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">v</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">range</span><span style="color:#393A34;"> ch1 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#AB5959;">&lt;-</span><span style="color:#393A34;">ch2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div>`,63),g=[c,t,r,y,F,i,A,u,E,h,d];function C(B,m,f,v,k,x){return p(),o("div",null,g)}const D=l(e,[["render",C]]);export{b as __pageData,D as default};
