タイプアサーション
- 型変換をすること
- interface型で受け取ると型の判断ができないため
intger型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="s">「fmt」を</span><span class="k">インポート</span> <span class="k">func </span> <span class="n">do </span><span class="p">(</span><span class="n">i </span> <span class="k">interface </span><span class="p">{}){ </span> <span class="c">//タイプアサーションインターフェイス型だとそのまま利用できないのでintergerへ変換する必要がある</span> <span class="n">ii </span> <span class="o">:= </span> <span class="n">i </span><span class="o">。</span><span class="p">(</span><span class="kt">int </span><span class="p">)</span> <span class="n">ii </span> <span class="o">* = </span> <span class="m">2 </span> <span class="n">fmt </span><span class="o">。</span><span class="n">Println </span><span class="p">(</span><span class="n">ii </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="k">var </span> <span class="n">i </span> <span class="k">interface </span><span class="p">{} </span> <span class="o">= </span> <span class="m">10 </span> <span class="n">do </span><span class="p">(</span><span class="n">i </span><span class="p">)</span> <span class="p">}</span> |
出力
1 |
20 |
文字列型
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="s">「fmt」を</span><span class="k">インポート</span> <span class="k">func </span> <span class="n">do </span><span class="p">(</span><span class="n">s </span> <span class="k">interface </span><span class="p">{}){ </span> <span class="n">ss </span> <span class="o">:= </span> <span class="n">s </span><span class="o">。</span><span class="p">(</span><span class="kt">文字列</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">ss </span> <span class="o">+ </span> <span class="s">"!" </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="n">do </span><span class="p">(</span><span class="s">"Mike" </span><span class="p">)</span> <span class="p">}</span> |
1 |
マイク! |
スイッチタイプ
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 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="s">「fmt」を</span><span class="k">インポート</span> <span class="k">func </span> <span class="n">do </span><span class="p">(</span><span class="n">i </span> <span class="k">interface </span><span class="p">{}){ </span> <span class="c">//いろいろな型に対応できるようにできるswitch type </span> <span class="k">switch </span> <span class="n">v </span> <span class="o">:= </span> <span class="n">i </span><span class="o">。</span><span class="p">(</span><span class="k">type </span><span class="p">){ </span> <span class="c">// intgerの場合</span> <span class="k">case </span> <span class="kt">int </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="n">v </span> <span class="o">* </span> <span class="m">2 </span><span class="p">)</span> <span class="c">// stringの場合</span> <span class="k">case </span> <span class="kt">string </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="n">v </span> <span class="o">+ </span> <span class="s">"!" </span><span class="p">)</span> <span class="c">//それ以外の</span> <span class="k">デフォルト</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">"%T%nがわからない" </span><span class="p">、</span> <span class="n">v </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="c">// string </span> <span class="n">do </span><span class="p">(</span><span class="s">"Mike" </span><span class="p">)</span> <span class="c">// integer </span> <span class="n">do </span><span class="p">(</span><span class="m">10 </span><span class="p">)</span> <span class="c">// boolean </span> <span class="n">do </span><span class="p">(</span><span class="no">true </span><span class="p">)</span> <span class="p">}</span> |
1 2 3 |
マイク! 20 %T%nが本当かわかりません |
ささら桁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="s">「fmt」を</span><span class="k">インポート</span> <span class="k">タイプ</span> <span class="n">Person </span> <span class="k">構造体</span><span class="p">{ </span> <span class="n">名前</span> <span class="kt">文字列</span> <span class="n">Age </span> <span class="kt">int </span> <span class="p">}</span> <span class="c">// String()で出力方法を変えられる</span> <span class="k">func </span> <span class="p">(</span><span class="n">p </span> <span class="n">Person </span><span class="p">)</span> <span class="n">String </span><span class="p">()</span> <span class="kt">string </span><span class="p">{ </span> <span class="c">//名前だけ返す(年齢は</span> <span class="k">返さ</span> <span class="c">ない)</span><span class="k">return </span><span class="n">fmt </span><span class="o">。</span><span class="n">sprintf </span><span class="p">(</span><span class="s">"私の名前は、体積%である" </span><span class="p">、</span> <span class="n">P </span><span class="o">。</span><span class="n">名前</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">mike </span> <span class="o">:= </span> <span class="n">Person </span><span class="p">{ </span><span class="s">"Mike" </span><span class="p">、</span> <span class="m">22 </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">マイク</span><span class="p">)</span> <span class="p">}</span> |
1 |
私の名前はマイクです |
カスタムエラー
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 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt" </span> <span class="p">)</span> <span class="k">タイプ</span> <span class="n">UserNotFound </span> <span class="k">構造体</span><span class="p">{ </span> <span class="n">ユーザー名</span> <span class="kt">文字列</span> <span class="p">}</span> <span class="k">func </span> <span class="p">(</span><span class="n">e </span> <span class="o">* </span><span class="n">UserNotFound </span><span class="p">)</span> <span class="n">Error </span><span class="p">()</span> <span class="kt">string </span><span class="p">{ </span> <span class="k">return </span> <span class="n">fmt </span><span class="o">。</span><span class="n">sprintf </span><span class="p">(</span><span class="s">"ユーザーが見つかりません:%のV" </span><span class="p">、</span> <span class="n">E </span><span class="o">。</span><span class="n">ユーザー名</span><span class="p">)</span> <span class="p">}</span> <span class="k">func </span> <span class="n">myFunc </span><span class="p">()</span> <span class="kt">error </span><span class="p">{ </span> <span class="c">// </span> <span class="n">何か</span><span class="kt">エラー</span><span class="c">が出たら</span><span class="n">ok </span> <span class="o">:= </span> <span class="no">false </span> <span class="c">// okならnil </span> <span class="k">if </span> <span class="n">ok </span> <span class="p">{ </span> <span class="k">return </span> <span class="no">nil </span> <span class="p">} </span> <span class="c">// </span> <span class="n">ユーザーが</span><span class="n">閲覧なかっ</span><span class="c">たら</span><span class="k">return </span> <span class="o">&</span><span class="n">UserNotFound </span><span class="p">{ </span><span class="n">Username </span><span class="o">:</span> <span class="s">"Mike" </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="k">if </span> <span class="n">err </span> <span class="o">:= </span> <span class="n">myFunc </span><span class="p">(); </span> <span class="n">err </span> <span class="o">!= </span> <span class="no">nil </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">err </span><span class="p">)</span> <span class="p">} </span> <span class="p">}</span> |
1 |
ユーザーが見つかりません:Mike |
ゴルーチン
- 並列処理
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 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt" </span> <span class="s">"sync" </span> <span class="p">)</span> <span class="k">FUNCの</span> <span class="n">ゴルーチン</span><span class="p">(</span><span class="n">S </span> <span class="kt">ストリング</span><span class="p">、</span> <span class="n">WG </span> <span class="o">* </span><span class="n">同期</span><span class="o">。</span><span class="n">WaitGroup </span><span class="p">){ </span> <span class="k">用</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">5 </span><span class="p">; </span> <span class="n">i </span><span class="o">++ </span><span class="p">{ </span> <span class="c">// time.Sleep(100 * time.Millisecond)</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="p">} </span> <span class="n">wg </span><span class="o">。</span><span class="n">完了</span><span class="p">()</span> <span class="p">}</span> <span class="k">func </span> <span class="n">normal </span><span class="p">(</span><span class="n">s </span> <span class="kt">string </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">5 </span><span class="p">; </span> <span class="n">i </span><span class="o">++ </span><span class="p">{ </span> <span class="c">// time.Sleep(100 * time.Millisecond)</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="p">} </span> <span class="p">}</span> <span class="k">func </span> <span class="n">main </span><span class="p">(){ </span> <span class="k">var </span> <span class="n">wg </span> <span class="n">sync </span><span class="o">。</span><span class="n">WaitGroup </span> <span class="n">wg </span><span class="o">。</span><span class="n">Add </span><span class="p">(</span><span class="m">1 </span><span class="p">)</span> <span class="c">//並列処理</span> <span class="k">go </span> <span class="n">goroutine </span><span class="p">(</span><span class="s">"world" </span><span class="p">、</span> <span class="o">&</span><span class="n">wg </span><span class="p">)</span> <span class="n">normal </span><span class="p">(</span><span class="s">"hello" </span><span class="p">)</span> <span class="c">// time.Sleep(2000 * time.Millisecond)</span> <span class="n">wg </span><span class="o">。</span><span class="n">待機</span><span class="p">()</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 9 10 |
こんにちは こんにちは こんにちは こんにちは こんにちは 世界 世界 世界 世界 世界 |