4 <h1><a name=
"go" id=
"go">Go
</a></h1>
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>
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"/>
18 <!-- SECTION "Go" [1-82] -->
19 <h2><a name=
"examples" id=
"examples">Examples
</a></h2>
22 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:go&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
25 all
<span class=
"sy0">:
</span> <span class=
"sy0">$
</span><span class=
"br0">(</span><span class=
"re2">PROG
</span><span class=
"br0">)</span>
27 <span class=
"sy0">%:
</span> <span class=
"sy0">%.
</span>8
28 8l <span class=
"sy0">-
</span>o
<span class=
"re0">$@
</span> <span class=
"re0">$
<</span>
30 <span class=
"sy0">%.
</span>8<span class=
"sy0">:
</span> <span class=
"sy0">%.
</span>go
31 8g
<span class=
"re0">$
<</span></pre>
34 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:go&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>
37 package main
<span class=
"co1">// executável principal sempre deve pertencer a este pacote
</span>
39 import .
<span class=
"st0">"fmt
"</span> <span class=
"co1">// importa funções de saída formatada (Printf)
</span>
41 <span class=
"co1">// função principal (como em C)
</span>
42 func main
<span class=
"br0">(</span><span class=
"br0">)</span> <span class=
"br0">{</span>
43 <span class=
"co1">//sem o . no import teríamos que utilizar fmt.Printf
</span>
44 Printf
<span class=
"br0">(</span><span class=
"st0">"hello, world
<span class=
"es1">\n
</span>"</span><span class=
"br0">)</span>
45 <span class=
"br0">}</span></pre>
48 <dt><a href=
"/~kssilveira/site/doku.php?do=export_code&id=main:go&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>
53 import
<span class=
"br0">(</span>
54 .
<span class=
"st0">"fmt
"</span>
55 <span class=
"st0">"crypto/rand
"</span>
56 <span class=
"st0">"time
"</span>
57 <span class=
"st0">"flag
"</span>
58 <span class=
"br0">)</span>
60 func random_between
<span class=
"br0">(</span>begin
<span class=
"sy0">,
</span> end int64
<span class=
"br0">)</span> int64
<span class=
"br0">{</span>
61 random_byte
<span class=
"sy0">:=
</span> make
<span class=
"br0">(</span><span class=
"br0">[</span><span class=
"br0">]</span>byte
<span class=
"sy0">,
</span> 1<span class=
"br0">)</span>
62 rand.
<span class=
"me1">Read
</span><span class=
"br0">(</span>random_byte
<span class=
"br0">)</span>
63 <span class=
"kw1">return
</span> int64
<span class=
"br0">(</span>random_byte
<span class=
"br0">[</span>0<span class=
"br0">]</span><span class=
"br0">)</span> <span class=
"sy0">%
</span> <span class=
"br0">(</span>end
<span class=
"sy0">-
</span> begin
<span class=
"sy0">+
</span> 1<span class=
"br0">)</span> <span class=
"sy0">+
</span> begin
64 <span class=
"br0">}</span>
66 func random_sleep
<span class=
"br0">(</span><span class=
"br0">)</span> <span class=
"br0">{</span>
67 time.
<span class=
"me1">Sleep
</span><span class=
"br0">(</span>random_between
<span class=
"br0">(</span>0<span class=
"sy0">,
</span> <span class=
"sy0">*
</span>MAX_SLEEP_TIME
<span class=
"br0">)</span> <span class=
"sy0">*
</span> 10e7
<span class=
"br0">)</span>
68 <span class=
"br0">}</span>
70 func send
<span class=
"br0">(</span>channel chan
<span class=
"kw4">int
</span><span class=
"sy0">,
</span> i
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"br0">{</span>
71 channel
<span class=
"sy0"><-
</span> i
72 <span class=
"br0">}</span>
74 func recv
<span class=
"br0">(</span>channel chan
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"kw4">int
</span> <span class=
"br0">{</span>
75 <span class=
"kw1">return
</span> <span class=
"sy0"><-
</span>channel
76 <span class=
"br0">}</span>
78 func signal
<span class=
"br0">(</span>channel chan
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"br0">{</span>
79 <span class=
"kw1">if
</span> len
<span class=
"br0">(</span>channel
<span class=
"br0">)</span> <span class=
"sy0"><</span> cap
<span class=
"br0">(</span>channel
<span class=
"br0">)</span> <span class=
"sy0">||
</span> cap
<span class=
"br0">(</span>channel
<span class=
"br0">)</span> <span class=
"sy0">==
</span> 0 <span class=
"br0">{</span>
80 channel
<span class=
"sy0"><-
</span> 0
81 <span class=
"br0">}</span>
82 <span class=
"br0">}</span>
84 func wait
<span class=
"br0">(</span>channel chan
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"br0">{</span>
85 <span class=
"sy0"><-
</span>channel
86 <span class=
"br0">}</span>
88 var
<span class=
"br0">(</span>
89 BUFFER_SIZE
<span class=
"sy0">=
</span> flag.
<span class=
"me1">Int
</span><span class=
"br0">(</span><span class=
"st0">"s
"</span><span class=
"sy0">,
</span> <span class=
"nu0">5</span><span class=
"sy0">,
</span> <span class=
"st0">"BUFFER_SIZE
"</span><span class=
"br0">)</span> <span class=
"co1">// flag -s
</span>
90 MAX_SLEEP_TIME
<span class=
"sy0">=
</span> flag.
<span class=
"me1">Int64
</span><span class=
"br0">(</span><span class=
"st0">"t
"</span><span class=
"sy0">,
</span> <span class=
"nu0">5</span><span class=
"sy0">,
</span> <span class=
"st0">"MAX_SLEEP_TIME
"</span><span class=
"br0">)</span> <span class=
"co1">// flag -t
</span>
91 MAX_PRODUCTIONS
<span class=
"sy0">=
</span> flag.
<span class=
"me1">Int
</span><span class=
"br0">(</span><span class=
"st0">"m
"</span><span class=
"sy0">,
</span> <span class=
"nu0">0</span><span class=
"sy0">,
</span> <span class=
"st0">"MAX_PRODUCTIONS,
0 for infinity
"</span><span class=
"br0">)</span> <span class=
"co1">// flag -m
</span>
92 N_PRODUCERS
<span class=
"sy0">=
</span> flag.
<span class=
"me1">Int
</span><span class=
"br0">(</span><span class=
"st0">"p
"</span><span class=
"sy0">,
</span> <span class=
"nu0">3</span><span class=
"sy0">,
</span> <span class=
"st0">"N_PRODUCERS
"</span><span class=
"br0">)</span> <span class=
"co1">// flag -p
</span>
93 N_CONSUMERS
<span class=
"sy0">=
</span> flag.
<span class=
"me1">Int
</span><span class=
"br0">(</span><span class=
"st0">"c
"</span><span class=
"sy0">,
</span> <span class=
"nu0">3</span><span class=
"sy0">,
</span> <span class=
"st0">"N_CONSUMERS
"</span><span class=
"br0">)</span> <span class=
"co1">// flag -c
</span>
94 pipe
<span class=
"sy0">=
</span> make
<span class=
"br0">(</span>chan
<span class=
"kw4">int
</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>BUFFER_SIZE
<span class=
"br0">)</span>
95 quit
<span class=
"sy0">=
</span> make
<span class=
"br0">(</span>chan
<span class=
"kw4">int
</span><span class=
"br0">)</span>
96 <span class=
"br0">)</span>
98 func producer
<span class=
"br0">(</span>id
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"br0">{</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"><</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">{</span>
100 Println
<span class=
"br0">(</span>id
<span class=
"sy0">,
</span> <span class=
"st0">"producing:
"</span><span class=
"sy0">,
</span> i
<span class=
"sy0">+
</span> id
<span class=
"sy0">*
</span> 1e5
<span class=
"br0">)</span>
101 send
<span class=
"br0">(</span>pipe
<span class=
"sy0">,
</span> i
<span class=
"sy0">+
</span> id
<span class=
"sy0">*
</span> 1e5
<span class=
"br0">)</span>
102 random_sleep
<span class=
"br0">(</span><span class=
"br0">)</span>
103 <span class=
"br0">}</span>
104 signal
<span class=
"br0">(</span>quit
<span class=
"br0">)</span>
105 <span class=
"br0">}</span>
107 func consumer
<span class=
"br0">(</span>id
<span class=
"kw4">int
</span><span class=
"br0">)</span> <span class=
"br0">{</span>
108 <span class=
"kw1">for
</span> <span class=
"br0">{</span>
109 v
<span class=
"sy0">:=
</span> recv
<span class=
"br0">(</span>pipe
<span class=
"br0">)</span>
110 <span class=
"kw1">if
</span> closed
<span class=
"br0">(</span>pipe
<span class=
"br0">)</span> <span class=
"br0">{</span> <span class=
"co1">// canal fechado?
</span>
111 <span class=
"kw2">break
</span>
112 <span class=
"br0">}</span>
113 Println
<span class=
"br0">(</span><span class=
"st0">"<span class=
"es1">\t
</span><span class=
"es1">\t
</span><span class=
"es1">\t
</span>"</span><span class=
"sy0">,
</span> id
<span class=
"sy0">,
</span> <span class=
"st0">"consuming:
"</span><span class=
"sy0">,
</span> v
<span class=
"br0">)</span>
114 random_sleep
<span class=
"br0">(</span><span class=
"br0">)</span>
115 <span class=
"br0">}</span>
116 signal
<span class=
"br0">(</span>quit
<span class=
"br0">)</span>
117 <span class=
"br0">}</span>
119 func main
<span class=
"br0">(</span><span class=
"br0">)</span> <span class=
"br0">{</span>
120 flag.
<span class=
"me1">Parse
</span><span class=
"br0">(</span><span class=
"br0">)</span> <span class=
"co1">// processa flags
</span>
121 Println
<span class=
"br0">(</span><span class=
"st0">"BUFFER_SIZE =
"</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>BUFFER_SIZE
<span class=
"br0">)</span>
122 Println
<span class=
"br0">(</span><span class=
"st0">"MAX_SLEEP_TIME =
"</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>MAX_SLEEP_TIME
<span class=
"br0">)</span>
123 Println
<span class=
"br0">(</span><span class=
"st0">"MAX_PRODUCTIONS =
"</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>MAX_PRODUCTIONS
<span class=
"br0">)</span>
124 Println
<span class=
"br0">(</span><span class=
"st0">"N_PRODUCERS =
"</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>N_PRODUCERS
<span class=
"br0">)</span>
125 Println
<span class=
"br0">(</span><span class=
"st0">"N_CONSUMERS =
"</span><span class=
"sy0">,
</span> <span class=
"sy0">*
</span>N_CONSUMERS
<span class=
"br0">)</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"><</span> <span class=
"sy0">*
</span>N_PRODUCERS
<span class=
"sy0">;
</span> i
<span class=
"sy0">++
</span> <span class=
"br0">{</span> <span class=
"co1">// cria produtores
</span>
127 go producer
<span class=
"br0">(</span>i
<span class=
"sy0">+
</span> 1<span class=
"br0">)</span>
128 <span class=
"br0">}</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"><</span> <span class=
"sy0">*
</span>N_CONSUMERS
<span class=
"sy0">;
</span> i
<span class=
"sy0">++
</span> <span class=
"br0">{</span> <span class=
"co1">// cria consumidores
</span>
130 go consumer
<span class=
"br0">(</span>i
<span class=
"sy0">+
</span> 1<span class=
"br0">)</span>
131 <span class=
"br0">}</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"><</span> <span class=
"sy0">*
</span>N_PRODUCERS
<span class=
"sy0">;
</span> i
<span class=
"sy0">++
</span> <span class=
"br0">{</span> <span class=
"co1">// espera o término dos produtores
</span>
133 wait
<span class=
"br0">(</span>quit
<span class=
"br0">)</span>
134 <span class=
"br0">}</span>
135 close
<span class=
"br0">(</span>pipe
<span class=
"br0">)</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"><</span> <span class=
"sy0">*
</span>N_CONSUMERS
<span class=
"sy0">;
</span> i
<span class=
"sy0">++
</span> <span class=
"br0">{</span> <span class=
"co1">// espera o término dos consumidores
</span>
137 wait
<span class=
"br0">(</span>quit
<span class=
"br0">)</span>
138 <span class=
"br0">}</span>
139 <span class=
"br0">}</span></pre>
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"/>
148 <!-- SECTION "Examples" [83-] -->