www.2017338.comGoSelect实例

算法 ww9992019com 发表于 1 周前

www.2017338.com开户请拨15974937077卖油条年入30万北京海淀区cTJZNl<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="9476">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>Go语言的选择(<code>select</code>)可等待多个通道操作。将<code>goroutine</code><code>channel</code><code>select</code>结合是Go语言的一个强大功能。</p>

<p>对于这个示例,将选择两个通道。<br>每个通道将在一段时间后开始接收值,以模拟阻塞在并发<code>goroutines</code>中执行的<code>RPC</code>操作。我们将使用<code>select</code>同时等待这两个值,在每个值到达时打印它们。</p>

<p>执行实例程序得到的值是“<code>one</code>”,然后是“<code>two</code>”。注意,总执行时间只有<code>1〜2</code>秒,因为<code>1-2</code>秒<code>Sleeps</code>同时执行。</p>

<blockquote>

<p>所有的示例代码,都放在 <code>F:\worksp\golang</code> 目录下。安装Go编程环境请参考:<a href="http://www.yiibai.com/go/go_environment.html">http://www.yiibai.com/go/go_environment.html</a> </p>

</blockquote>

<p><code>select.go</code>的完整代码如下所示 -</p>

<pre><code class="lang-go">package main

import "time"

import "fmt"

func main() {

// For our example we'll select across two channels.

c1 := make(chan string)

c2 := make(chan string)



// Each channel will receive a value after some amount

// of time, to simulate e.g. blocking RPC operations

// executing in concurrent goroutines.

go func() {

    time.Sleep(time.Second * 1)

    c1 &lt;- "one"

}()

go func() {

    time.Sleep(time.Second * 2)

    c2 &lt;- "two"

}()



// We'll use `select` to await both of these values

// simultaneously, printing each one as it arrives.

for i := 0; i &lt; 2; i++ {

    select {

    case msg1 := &lt;-c1:

        fmt.Println("received", msg1)

    case msg2 := &lt;-c2:

        fmt.Println("received", msg2)

    }

}

}

</code></pre>

<p>执行上面代码,将得到以下输出结果 - </p>

<pre><code class="lang-go">F:\worksp\golang&gt;go run select.go

received one

received two

</code></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Go通道路线实例"><i class="layui-icon"></i>
                            Go通道路线实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Go超时(timeouts)实例"><i class="layui-icon"></i>
                            Go超时(timeouts)实例</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复