クロージャー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="c">// インクリメント</span> <span class="k">func</span> <span class="n">incrementGenenrator</span><span class="p">()</span> <span class="k">func</span><span class="p">()</span> <span class="kt">int</span> <span class="p">{</span> <span class="n">x</span> <span class="o">:=</span> <span class="m">0</span> <span class="k">return</span> <span class="k">func</span><span class="p">()</span> <span class="kt">int</span> <span class="p">{</span> <span class="n">x</span><span class="o">++</span> <span class="k">return</span> <span class="n">x</span> <span class="p">}</span> <span class="p">}</span> <span class="c">// 円の面積</span> <span class="k">func</span> <span class="n">circleArea</span><span class="p">(</span><span class="n">pi</span> <span class="kt">float64</span><span class="p">)</span> <span class="k">func</span><span class="p">(</span><span class="n">radius</span> <span class="kt">float64</span><span class="p">)</span> <span class="kt">float64</span> <span class="p">{</span> <span class="k">return</span> <span class="k">func</span><span class="p">(</span><span class="n">radius</span> <span class="kt">float64</span><span class="p">)</span> <span class="kt">float64</span> <span class="p">{</span> <span class="k">return</span> <span class="n">pi</span> <span class="o">*</span> <span class="n">radius</span> <span class="o">*</span> <span class="n">radius</span> <span class="p">}</span> <span class="p">}</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">counter</span> <span class="o">:=</span> <span class="n">incrementGenenrator</span><span class="p">()</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">counter</span><span class="p">())</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">counter</span><span class="p">())</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">counter</span><span class="p">())</span> <span class="n">c1</span> <span class="o">:=</span> <span class="n">circleArea</span><span class="p">(</span><span class="m">3.14</span><span class="p">)</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">c1</span><span class="p">(</span><span class="m">2</span><span class="p">))</span> <span class="n">c2</span> <span class="o">:=</span> <span class="n">circleArea</span><span class="p">(</span><span class="m">3</span><span class="p">)</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">c2</span><span class="p">(</span><span class="m">2</span><span class="p">))</span> <span class="p">}</span> |
1 2 3 4 5 |
1 2 3 12.56 12 |
可変長引数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="c">// func foo(param1, param2 int) とするとfoo(10, 20)の形でしか呼べない</span> <span class="k">func</span> <span class="n">foo</span><span class="p">(</span><span class="n">params</span> <span class="o">...</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">),</span> <span class="n">params</span><span class="p">)</span> <span class="c">// forの中でひとつひとつ呼ぶ</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">param</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">params</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">param</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">foo</span><span class="p">()</span> <span class="n">foo</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">20</span><span class="p">)</span> <span class="n">foo</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">20</span><span class="p">,</span> <span class="m">30</span><span class="p">)</span> <span class="n">s</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">}</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="n">foo</span><span class="p">(</span><span class="n">s</span><span class="o">...</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
0 [] 2 [10 20] 10 20 3 [10 20 30] 10 20 30 [1 2 3] 3 [1 2 3] 1 2 3 |
if文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="c">// if elseif else</span> <span class="n">num</span> <span class="o">:=</span> <span class="m">9</span> <span class="k">if</span> <span class="n">num</span><span class="o">%</span><span class="m">2</span> <span class="o">==</span> <span class="m">0</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"by 2"</span><span class="p">)</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="n">num</span><span class="o">%</span><span class="m">3</span> <span class="o">==</span> <span class="m">0</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"by 3"</span><span class="p">)</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"else"</span><span class="p">)</span> <span class="p">}</span> <span class="c">// and条件 両方いっちしてTrue</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">:=</span> <span class="m">10</span><span class="p">,</span> <span class="m">12</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="m">10</span> <span class="o">&&</span> <span class="n">y</span> <span class="o">==</span> <span class="m">10</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"&&"</span><span class="p">)</span> <span class="p">}</span> <span class="c">// or条件 どちらか一致していればTrue</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="m">10</span> <span class="o">||</span> <span class="n">y</span> <span class="o">==</span> <span class="m">10</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"&&"</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 |
by 3 && |
関数を使う
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="c">// 0で割り切れればOK関数</span> <span class="k">func</span> <span class="n">by2</span><span class="p">(</span><span class="n">num</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">string</span> <span class="p">{</span> <span class="k">if</span> <span class="n">num</span><span class="o">%</span><span class="m">2</span> <span class="o">==</span> <span class="m">0</span> <span class="p">{</span> <span class="k">return</span> <span class="s">"ok"</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="s">"no"</span> <span class="p">}</span> <span class="p">}</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">result</span> <span class="o">:=</span> <span class="n">by2</span><span class="p">(</span><span class="m">10</span><span class="p">)</span> <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="s">"ok"</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Great"</span><span class="p">)</span> <span class="p">}</span> <span class="c">// 1行で書く場合</span> <span class="k">if</span> <span class="n">result2</span> <span class="o">:=</span> <span class="n">by2</span><span class="p">(</span><span class="m">10</span><span class="p">);</span> <span class="n">result2</span> <span class="o">==</span> <span class="s">"ok"</span> <span class="p">{</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Great 2"</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 |
Great Great 2 |