backup de julho
[h2N7SspZmY.git] / data / cache / 4 / 4e30f1a18e08341475fd9fb1d207e30d.xhtml
blob723f50e22566a4b75d4d6c70a2b52615aa19e8e4
4 <h1><a name="go" id="go">Go</a></h1>
5 <div class="level1">
7 <p>
9 <a href="http://golang.org/" class="urlextern" title="http://golang.org/" rel="nofollow">Go</a>: <a href="http://en.wikipedia.org/wiki/Go_(programming_language)" class="interwiki iw_wp" title="http://en.wikipedia.org/wiki/Go_(programming_language)">Go</a>
10 </p>
11 <div class="plugin_uparrow">
12 <a href="#" title="Back to top">
13 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
14 </a>
15 </div>
17 </div>
18 <!-- SECTION "Go" [1-82] -->
19 <h2><a name="examples" id="examples">Examples</a></h2>
20 <div class="level2">
21 <dl class="file">
22 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=main:go&amp;codeblock=0" title="Download Snippet" class="mediafile mf_">makefile</a></dt>
23 <dd><pre class="code file make">PROG <span class="sy0">=</span> prog_name
24 &nbsp;
25 all<span class="sy0">:</span> <span class="sy0">$</span><span class="br0">&#40;</span><span class="re2">PROG</span><span class="br0">&#41;</span>
26 &nbsp;
27 <span class="sy0">%:</span> <span class="sy0">%.</span>8
28 8l <span class="sy0">-</span>o <span class="re0">$@</span> <span class="re0">$&lt;</span>
29 &nbsp;
30 <span class="sy0">%.</span>8<span class="sy0">:</span> <span class="sy0">%.</span>go
31 8g <span class="re0">$&lt;</span></pre>
32 </dd></dl>
33 <dl class="file">
34 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=main:go&amp;codeblock=1" title="Download Snippet" class="mediafile mf_go">hello.go</a></dt>
35 <dd><pre class="code file c"><span class="coMULTI">/* hello */</span>
36 &nbsp;
37 package main <span class="co1">// executável principal sempre deve pertencer a este pacote</span>
38 &nbsp;
39 import . <span class="st0">&quot;fmt&quot;</span> <span class="co1">// importa funções de saída formatada (Printf)</span>
40 &nbsp;
41 <span class="co1">// função principal (como em C)</span>
42 func main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
43 <span class="co1">//sem o . no import teríamos que utilizar fmt.Printf</span>
44 Printf<span class="br0">&#40;</span><span class="st0">&quot;hello, world<span class="es1">\n</span>&quot;</span><span class="br0">&#41;</span>
45 <span class="br0">&#125;</span></pre>
46 </dd></dl>
47 <dl class="file">
48 <dt><a href="/~kssilveira/site/doku.php?do=export_code&amp;id=main:go&amp;codeblock=2" title="Download Snippet" class="mediafile mf_go">producer_consumer.go</a></dt>
49 <dd><pre class="code file c"><span class="coMULTI">/* pc_channel_multi.go */</span>
50 &nbsp;
51 package main
52 &nbsp;
53 import <span class="br0">&#40;</span>
54 . <span class="st0">&quot;fmt&quot;</span>
55 <span class="st0">&quot;crypto/rand&quot;</span>
56 <span class="st0">&quot;time&quot;</span>
57 <span class="st0">&quot;flag&quot;</span>
58 <span class="br0">&#41;</span>
59 &nbsp;
60 func random_between <span class="br0">&#40;</span>begin<span class="sy0">,</span> end int64<span class="br0">&#41;</span> int64 <span class="br0">&#123;</span>
61 random_byte <span class="sy0">:=</span> make<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#93;</span>byte<span class="sy0">,</span> 1<span class="br0">&#41;</span>
62 rand.<span class="me1">Read</span><span class="br0">&#40;</span>random_byte<span class="br0">&#41;</span>
63 <span class="kw1">return</span> int64<span class="br0">&#40;</span>random_byte<span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>end <span class="sy0">-</span> begin <span class="sy0">+</span> 1<span class="br0">&#41;</span> <span class="sy0">+</span> begin
64 <span class="br0">&#125;</span>
65 &nbsp;
66 func random_sleep <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
67 time.<span class="me1">Sleep</span><span class="br0">&#40;</span>random_between<span class="br0">&#40;</span>0<span class="sy0">,</span> <span class="sy0">*</span>MAX_SLEEP_TIME<span class="br0">&#41;</span> <span class="sy0">*</span> 10e7<span class="br0">&#41;</span>
68 <span class="br0">&#125;</span>
69 &nbsp;
70 func send <span class="br0">&#40;</span>channel chan <span class="kw4">int</span><span class="sy0">,</span> i <span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
71 channel <span class="sy0">&lt;-</span> i
72 <span class="br0">&#125;</span>
73 &nbsp;
74 func recv <span class="br0">&#40;</span>channel chan <span class="kw4">int</span><span class="br0">&#41;</span> <span class="kw4">int</span> <span class="br0">&#123;</span>
75 <span class="kw1">return</span> <span class="sy0">&lt;-</span>channel
76 <span class="br0">&#125;</span>
77 &nbsp;
78 func signal <span class="br0">&#40;</span>channel chan <span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
79 <span class="kw1">if</span> len<span class="br0">&#40;</span>channel<span class="br0">&#41;</span> <span class="sy0">&lt;</span> cap<span class="br0">&#40;</span>channel<span class="br0">&#41;</span> <span class="sy0">||</span> cap<span class="br0">&#40;</span>channel<span class="br0">&#41;</span> <span class="sy0">==</span> 0 <span class="br0">&#123;</span>
80 channel <span class="sy0">&lt;-</span> 0
81 <span class="br0">&#125;</span>
82 <span class="br0">&#125;</span>
83 &nbsp;
84 func wait <span class="br0">&#40;</span>channel chan <span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
85 <span class="sy0">&lt;-</span>channel
86 <span class="br0">&#125;</span>
87 &nbsp;
88 var <span class="br0">&#40;</span>
89 BUFFER_SIZE <span class="sy0">=</span> flag.<span class="me1">Int</span><span class="br0">&#40;</span><span class="st0">&quot;s&quot;</span><span class="sy0">,</span> <span class="nu0">5</span><span class="sy0">,</span> <span class="st0">&quot;BUFFER_SIZE&quot;</span><span class="br0">&#41;</span> <span class="co1">// flag -s</span>
90 MAX_SLEEP_TIME <span class="sy0">=</span> flag.<span class="me1">Int64</span><span class="br0">&#40;</span><span class="st0">&quot;t&quot;</span><span class="sy0">,</span> <span class="nu0">5</span><span class="sy0">,</span> <span class="st0">&quot;MAX_SLEEP_TIME&quot;</span><span class="br0">&#41;</span> <span class="co1">// flag -t</span>
91 MAX_PRODUCTIONS <span class="sy0">=</span> flag.<span class="me1">Int</span><span class="br0">&#40;</span><span class="st0">&quot;m&quot;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="st0">&quot;MAX_PRODUCTIONS, 0 for infinity&quot;</span><span class="br0">&#41;</span> <span class="co1">// flag -m</span>
92 N_PRODUCERS <span class="sy0">=</span> flag.<span class="me1">Int</span><span class="br0">&#40;</span><span class="st0">&quot;p&quot;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="st0">&quot;N_PRODUCERS&quot;</span><span class="br0">&#41;</span> <span class="co1">// flag -p</span>
93 N_CONSUMERS <span class="sy0">=</span> flag.<span class="me1">Int</span><span class="br0">&#40;</span><span class="st0">&quot;c&quot;</span><span class="sy0">,</span> <span class="nu0">3</span><span class="sy0">,</span> <span class="st0">&quot;N_CONSUMERS&quot;</span><span class="br0">&#41;</span> <span class="co1">// flag -c</span>
94 pipe <span class="sy0">=</span> make<span class="br0">&#40;</span>chan <span class="kw4">int</span><span class="sy0">,</span> <span class="sy0">*</span>BUFFER_SIZE<span class="br0">&#41;</span>
95 quit <span class="sy0">=</span> make<span class="br0">&#40;</span>chan <span class="kw4">int</span><span class="br0">&#41;</span>
96 <span class="br0">&#41;</span>
97 &nbsp;
98 func producer <span class="br0">&#40;</span>id <span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
99 <span class="kw1">for</span> i <span class="sy0">:=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> <span class="sy0">*</span>MAX_PRODUCTIONS <span class="sy0">||</span> <span class="sy0">*</span>MAX_PRODUCTIONS <span class="sy0">==</span> <span class="nu0">0</span><span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#123;</span>
100 Println<span class="br0">&#40;</span>id<span class="sy0">,</span> <span class="st0">&quot;producing:&quot;</span><span class="sy0">,</span> i <span class="sy0">+</span> id <span class="sy0">*</span> 1e5<span class="br0">&#41;</span>
101 send<span class="br0">&#40;</span>pipe<span class="sy0">,</span> i <span class="sy0">+</span> id <span class="sy0">*</span> 1e5<span class="br0">&#41;</span>
102 random_sleep<span class="br0">&#40;</span><span class="br0">&#41;</span>
103 <span class="br0">&#125;</span>
104 signal<span class="br0">&#40;</span>quit<span class="br0">&#41;</span>
105 <span class="br0">&#125;</span>
106 &nbsp;
107 func consumer <span class="br0">&#40;</span>id <span class="kw4">int</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
108 <span class="kw1">for</span> <span class="br0">&#123;</span>
109 v <span class="sy0">:=</span> recv<span class="br0">&#40;</span>pipe<span class="br0">&#41;</span>
110 <span class="kw1">if</span> closed<span class="br0">&#40;</span>pipe<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// canal fechado?</span>
111 <span class="kw2">break</span>
112 <span class="br0">&#125;</span>
113 Println<span class="br0">&#40;</span><span class="st0">&quot;<span class="es1">\t</span><span class="es1">\t</span><span class="es1">\t</span>&quot;</span><span class="sy0">,</span> id<span class="sy0">,</span> <span class="st0">&quot;consuming:&quot;</span><span class="sy0">,</span> v<span class="br0">&#41;</span>
114 random_sleep<span class="br0">&#40;</span><span class="br0">&#41;</span>
115 <span class="br0">&#125;</span>
116 signal<span class="br0">&#40;</span>quit<span class="br0">&#41;</span>
117 <span class="br0">&#125;</span>
118 &nbsp;
119 func main <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
120 flag.<span class="me1">Parse</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="co1">// processa flags</span>
121 Println<span class="br0">&#40;</span><span class="st0">&quot;BUFFER_SIZE =&quot;</span><span class="sy0">,</span> <span class="sy0">*</span>BUFFER_SIZE<span class="br0">&#41;</span>
122 Println<span class="br0">&#40;</span><span class="st0">&quot;MAX_SLEEP_TIME =&quot;</span><span class="sy0">,</span> <span class="sy0">*</span>MAX_SLEEP_TIME<span class="br0">&#41;</span>
123 Println<span class="br0">&#40;</span><span class="st0">&quot;MAX_PRODUCTIONS =&quot;</span><span class="sy0">,</span> <span class="sy0">*</span>MAX_PRODUCTIONS<span class="br0">&#41;</span>
124 Println<span class="br0">&#40;</span><span class="st0">&quot;N_PRODUCERS =&quot;</span><span class="sy0">,</span> <span class="sy0">*</span>N_PRODUCERS<span class="br0">&#41;</span>
125 Println<span class="br0">&#40;</span><span class="st0">&quot;N_CONSUMERS =&quot;</span><span class="sy0">,</span> <span class="sy0">*</span>N_CONSUMERS<span class="br0">&#41;</span>
126 <span class="kw1">for</span> i <span class="sy0">:=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> <span class="sy0">*</span>N_PRODUCERS<span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#123;</span> <span class="co1">// cria produtores</span>
127 go producer<span class="br0">&#40;</span>i <span class="sy0">+</span> 1<span class="br0">&#41;</span>
128 <span class="br0">&#125;</span>
129 <span class="kw1">for</span> i <span class="sy0">:=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> <span class="sy0">*</span>N_CONSUMERS<span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#123;</span> <span class="co1">// cria consumidores</span>
130 go consumer<span class="br0">&#40;</span>i <span class="sy0">+</span> 1<span class="br0">&#41;</span>
131 <span class="br0">&#125;</span>
132 <span class="kw1">for</span> i <span class="sy0">:=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> <span class="sy0">*</span>N_PRODUCERS<span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#123;</span> <span class="co1">// espera o término dos produtores</span>
133 wait<span class="br0">&#40;</span>quit<span class="br0">&#41;</span>
134 <span class="br0">&#125;</span>
135 close<span class="br0">&#40;</span>pipe<span class="br0">&#41;</span> <span class="co1">// fecha comunicação</span>
136 <span class="kw1">for</span> i <span class="sy0">:=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0">&lt;</span> <span class="sy0">*</span>N_CONSUMERS<span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">&#123;</span> <span class="co1">// espera o término dos consumidores</span>
137 wait<span class="br0">&#40;</span>quit<span class="br0">&#41;</span>
138 <span class="br0">&#125;</span>
139 <span class="br0">&#125;</span></pre>
140 </dd></dl>
141 <div class="plugin_uparrow">
142 <a href="#" title="Back to top">
143 <img src="lib/plugins/uparrow/images/tango-small.png" alt="Back to top"/>
144 </a>
145 </div>
147 </div>
148 <!-- SECTION "Examples" [83-] -->