宿題
問1: continueとbreakの違いは?
- continue → ループ中の今差し掛かっている処理は実行されず、次のループへ
- break → ループ終了
問2: Go言語におけるスイッチ文ではケース式に合致したらそのケースしか実行されない。しかし他の言語ではどうなっているか?複数のケースが実行されてしまうことがある。この言語仕様の名称はなにか?
- 名称 fallthrough(落ちるという意味)
- bashだとGoと同じな気がします
問3: 問2における機能をGo言語で実装する方法を答えよ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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">i</span> <span class="o">:=</span> <span class="m">1</span> <span class="k">switch</span> <span class="p">{</span> <span class="k">case</span> <span class="n">i</span> <span class="o"><</span> <span class="m">5</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"<5"</span><span class="p">)</span> <span class="k">fallthrough</span> <span class="k">case</span> <span class="n">i</span> <span class="o"><</span> <span class="m">7</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"<7"</span><span class="p">)</span> <span class="k">default</span><span class="o">:</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 |
<5 <7 |
遅延実行
- 関数内の最後に実行される
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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">getOsName</span><span class="p">()</span> <span class="kt">string</span> <span class="p">{</span> <span class="k">return</span> <span class="s">"windows"</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="c">// os := "windows"</span> <span class="n">os</span> <span class="o">:=</span> <span class="n">getOsName</span><span class="p">()</span> <span class="k">switch</span> <span class="n">os</span> <span class="p">{</span> <span class="k">case</span> <span class="s">"mac"</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Mac"</span><span class="p">)</span> <span class="k">case</span> <span class="s">"windows"</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Windows"</span><span class="p">)</span> <span class="c">// defaultなくても大丈夫</span> <span class="k">default</span><span class="o">:</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"Default"</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 3 4 |
hello foo world foo hello world |
ファイルを開く処理などで使う
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"os"</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="c">// ファイルを開く</span> <span class="n">file</span><span class="p">,</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">os</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">"./test.go"</span><span class="p">)</span> <span class="c">// 処理終了後にファイルを閉じる必要があるのでdeferを使う</span> <span class="k">defer</span> <span class="n">file</span><span class="o">.</span><span class="n">Close</span><span class="p">()</span> <span class="n">data</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">byte</span><span class="p">,</span> <span class="m">100</span><span class="p">)</span> <span class="n">file</span><span class="o">.</span><span class="n">Read</span><span class="p">(</span><span class="n">data</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="kt">string</span><span class="p">(</span><span class="n">data</span><span class="p">))</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 9 10 |
package main import ( "fmt" "os" ) func main() { // ファイルを開く file, _ := |
ロギング
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"log"</span> <span class="s">"os"</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="c">// でたらめなファイルを開く</span> <span class="n">_</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">os</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">"aaaaa"</span><span class="p">)</span> <span class="c">// エラー処理</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatalln</span><span class="p">(</span><span class="s">"exit"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="p">}</span> <span class="n">log</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"logging"</span><span class="p">)</span> <span class="n">log</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"%T %v"</span><span class="p">,</span> <span class="s">"test"</span><span class="p">,</span> <span class="s">"test"</span><span class="p">)</span> <span class="c">// Fatalが実行されると処理が終了する</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatalf</span><span class="p">(</span><span class="s">"%T %v"</span><span class="p">,</span> <span class="s">"test"</span><span class="p">,</span> <span class="s">"test"</span><span class="p">)</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatalln</span><span class="p">(</span><span class="s">"error"</span><span class="p">)</span> <span class="p">}</span> |
1 2 |
2020/06/18 21:50:01 exit open aaaaa: The system cannot find the file specified. |
ファイル書き込み
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 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"io"</span> <span class="s">"log"</span> <span class="s">"os"</span> <span class="p">)</span> <span class="c">// loggin関数</span> <span class="k">func</span> <span class="n">LoggingSettings</span><span class="p">(</span><span class="n">logFile</span> <span class="kt">string</span><span class="p">)</span> <span class="p">{</span> <span class="c">// 666、読み書き追記できるように作る</span> <span class="n">logfile</span><span class="p">,</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">os</span><span class="o">.</span><span class="n">OpenFile</span><span class="p">(</span><span class="n">logFile</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDWR</span><span class="o">|</span><span class="n">os</span><span class="o">.</span><span class="n">O_CREATE </span><span class="o">|</span><span class="n">os</span><span class="o">.</span><span class="n">O_APPEND</span><span class="p">,</span> <span class="m">0666</span><span class="p">)</span> <span class="c">// Stdoutで標準出力もしつつlogging</span> <span class="n">multilogFile</span> <span class="o">:=</span> <span class="n">io</span><span class="o">.</span><span class="n">MultiWriter</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">Stdout</span><span class="p">,</span> <span class="n">logfile</span><span class="p">)</span> <span class="c">// 時間とか、Llongfileは絶対パス表記</span> <span class="n">log</span><span class="o">.</span><span class="n">SetFlags</span><span class="p">(</span><span class="n">log</span><span class="o">.</span><span class="n">Ldate</span> <span class="o">|</span> <span class="n">log</span><span class="o">.</span><span class="n">Ltime</span> <span class="o">|</span> <span class="n">log</span><span class="o">.</span><span class="n">Llongfile</span><span class="p">)</span> <span class="n">log</span><span class="o">.</span><span class="n">SetOutput</span><span class="p">(</span><span class="n">multilogFile</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">LoggingSettings</span><span class="p">(</span><span class="s">"test.log"</span><span class="p">)</span> <span class="c">// でたらめなファイルを開く</span> <span class="n">_</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">os</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">"aaaaa"</span><span class="p">)</span> <span class="c">// エラー処理</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatalln</span><span class="p">(</span><span class="s">"exit"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |