golang goroutine顺序输出
发布时间:2022-03-26 编辑:jiaochengji.com
教程集为您提供golang goroutine顺序输出等资源,欢迎您收藏本站,我们将为您提供最新的golang goroutine顺序输出资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg><h1 id="golang-goroutine顺序输出">golang goroutine顺序输出</h1>
range字符串,使用goroutine打印,因为goroutine随机执行
<pre class="prettyprint"><code class=" hljs go"><span class="hljs-keyword">for</span> _, v := <span class="hljs-keyword">range</span> str { <span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>() { fmt.Println(<span class="hljs-typename">string</span>(v)) }() }</code></pre>输出:
<pre class="prettyprint"><code class=" hljs ">5 5 5 5 5</code></pre>可以使用chan顺序输出
<pre class="prettyprint"><code class=" hljs go"> <span class="hljs-keyword">for</span> _, c := <span class="hljs-keyword">range</span> str{ ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-typename">rune</span>) <span class="hljs-keyword">go</span> <span class="hljs-keyword">func</span>(ch <-<span class="hljs-keyword">chan</span> <span class="hljs-typename">rune</span>) { key := <-ch fmt.Println(<span class="hljs-typename">string</span>(key)) }(ch) ch <- c }</code></pre>输出:
<pre class="prettyprint"><code class=" hljs ">1 2 3 4 5</code></pre> 到此这篇关于“golang goroutine顺序输出”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!您可能感兴趣的文章:
golang goroutine顺序输出
golang 深入浅出之 goroutine 理解
关于 goroutine 调度问题
22Go常见的并发模式和并发模型
Goroutine的调度分析(一)
简单理解 Goroutine 是如何工作的
[Go 教程系列笔记] goroutine(协程)
Go的隐秘世界:有Thread为啥还要Goroutine
goroutine
Golang基础--常见坑
上一篇:真的理解go interface了吗?
下一篇:Go语言入门教程-介绍和安装
[关闭]