for
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 |
<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="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="m">10</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span> <span class="p">{</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</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="s">"continue"</span><span class="p">)</span> <span class="c">// ループ内のcontinue以降は実行されず、次のループへ行く</span> <span class="k">continue</span> <span class="p">}</span> <span class="k">if</span> <span class="n">i</span> <span class="o">></span> <span class="m">5</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">"break"</span><span class="p">)</span> <span class="c">// ループ終了</span> <span class="k">break</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">i</span><span class="p">)</span> <span class="p">}</span> <span class="c">// 省略版</span> <span class="n">sum</span> <span class="o">:=</span> <span class="m">1</span> <span class="k">for</span> <span class="n">sum</span> <span class="o"><</span> <span class="m">10</span> <span class="p">{</span> <span class="n">sum</span> <span class="o">+=</span> <span class="n">sum</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">sum</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="n">sum</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 9 10 11 |
0 1 2 continue 4 5 break 2 4 8 16 |
range
- 要素数分ループ
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 33 34 35 36 37 38 |
<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="n">l</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="s">"python"</span><span class="p">,</span> <span class="s">"go"</span><span class="p">,</span> <span class="s">"java"</span><span class="p">}</span> <span class="k">for</span> <span class="n">i</span> <span class="o">:=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">);</span> <span class="n">i</span><span class="o">++</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">i</span><span class="p">,</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="p">}</span> <span class="c">// 短縮版 lの中身のインデックスを入れてくれる</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">l</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">i</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span> <span class="c">// vだけ使いたい場合</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">l</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">v</span><span class="p">)</span> <span class="p">}</span> <span class="c">// map型</span> <span class="n">m</span> <span class="o">:=</span> <span class="k">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="s">"apple"</span><span class="o">:</span> <span class="m">100</span><span class="p">,</span> <span class="s">"banana"</span><span class="o">:</span> <span class="m">200</span><span class="p">}</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">m</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">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span> <span class="c">// keyだけ取り出す</span> <span class="k">for</span> <span class="n">k</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">m</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">k</span><span class="p">)</span> <span class="p">}</span> <span class="c">// valueだけを取り出す</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">m</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">v</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
0 python 1 go 2 java 0 python 1 go 2 java python go java apple 100 banana 200 apple banana 100 200 |
switch文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package main import "fmt" func getOsName() string { return "windows" } func main() { // os := "windows" os := getOsName() switch os { case "mac": fmt.Println("Mac") case "windows": fmt.Println("Windows") // defaultなくても大丈夫 default: fmt.Println("Default") } } |
1 |
Windows |
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 |
package main import ( "fmt" "time" ) func getOsName() string { return "linux" } func main() { // switch文の中だけ変数が有効 短縮版 switch os := getOsName(); os { case "mac": fmt.Println("Mac") case "windows": fmt.Println("Windows") default: fmt.Println("Default", os) } // timeオブジェクトのメソッド部分も利用できる t := time.Now() fmt.Println(t.Hour()) switch { case t.Hour() < 12: fmt.Println("Morning") case t.Hour() < 17: fmt.Println("Afternoon") } } |
1 2 3 |
Default linux 8 Morning |