gobyexample/public/channel-buffering
2019-10-15 15:02:01 +03:00

163 lines
5.9 KiB
Plaintext
Generated
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go в примерах: Буферизированный канал (Channel Buffering)</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'channels';
}
if (e.key == "ArrowRight") {
window.location.href = 'channel-synchronization';
}
}
</script>
<body>
<div class="example" id="channel-buffering">
<h2><a href="./">Go в примерах</a>: Буферизированный канал (Channel Buffering)</h2>
<table>
<tr>
<td class="docs">
<p>По умолчанию каналы <em>не буферизованы</em>, это означает,
что они будут принимать отправления (<code>chan &lt;-</code>), только
если есть соответствующий прием (<code>&lt;- chan</code>), готовый
принять отправленное значение. <em>Буферизованные каналы</em>
принимают ограниченное количество значений без
соответствующего приемника для этих значений.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/HNVSsVpnHXN" target="_blank"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Здесь мы <code>создаем</code> канал строк с буфером до 2
значений.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">messages</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">chan</span> <span class="kt">string</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Т.к. этот канал буферизирован, мы можем послать
значения в канал без соответствующего одновременного
получения.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">messages</span> <span class="o">&lt;-</span> <span class="s">&quot;buffered&quot;</span>
<span class="nx">messages</span> <span class="o">&lt;-</span> <span class="s">&quot;channel&quot;</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Позже мы можем получить эти значения как обычно.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="o">&lt;-</span><span class="nx">messages</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="o">&lt;-</span><span class="nx">messages</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> go run channel-buffering.go
<span class="go">buffered</span>
<span class="go">channel</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Следующий пример: <a href="channel-synchronization">Синхронизация канала (Channel Synchronization)</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/channel-buffering">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
<br/>
переведено Nick S. | <a href="https://github.com/badkaktus/gobyexample">исходники</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' messages := make(chan string, 2)\u000A');codeLines.push(' messages \x3C- \"buffered\"\u000A messages \x3C- \"channel\"\u000A');codeLines.push(' fmt.Println(\x3C-messages)\u000A fmt.Println(\x3C-messages)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>