HAMC
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 39 40 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"crypto / hmac" </span> <span class="s">"crypto / sha256" </span> <span class="s">"encoding / hex" </span> <span class="s">"fmt" </span> <span class="p">)</span> <span class="c">//サーバーサイドで正しいクライアントのものかを判別</span> <span class="k">var </span> <span class="n">DB </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">string </span><span class="p">{ </span> <span class="s">"User1Key" </span><span class="o">:</span> <span class="s">"User1Secret" </span><span class="p">、</span> <span class="s">"User2Key" </span><span class="o">:</span> <span class="s">"User2Secret" </span><span class="p">、</span> <span class="p">}</span> <span class="k">func </span> <span class="n">Server </span><span class="p">(</span><span class="n">apiKey </span><span class="p">、</span> <span class="n">sign </span> <span class="kt">string </span><span class="p">、</span> <span class="n">data </span> <span class="p">[] </span><span class="kt">byte </span><span class="p">)</span> <span class="p">{ </span> <span class="n">apiSecret </span> <span class="o">:= </span> <span class="n">DB </span><span class="p">[ </span><span class="n">apiKey </span><span class="p">] </span> <span class="c">// sha256の同じアルゴリズムを使う</span> <span class="n">h </span> <span class="o">:= </span> <span class="n">hmac </span><span class="o">。</span><span class="n">新規</span><span class="p">(</span><span class="n">sha256 </span><span class="o">。</span><span class="n">新規</span><span class="p">、</span> <span class="p">[] </span><span class="kt">バイト</span><span class="p">(</span><span class="n">apiSecret </span><span class="p">))</span> <span class="n">h </span><span class="o">。</span><span class="n">書き込み</span><span class="p">(</span><span class="n">データ</span><span class="p">)</span> <span class="n">expectedHMAC </span> <span class="o">:= </span> <span class="n">hex </span><span class="o">。</span><span class="n">EncodeToString </span><span class="p">(</span><span class="n">H </span><span class="o">。</span><span class="n">和</span><span class="p">(</span><span class="no">ゼロ</span><span class="p">))</span> <span class="c">//同じアルゴリズムでエンコードしたhmacが一致するかを確認</span> <span class="n">fmt </span><span class="o">。</span><span class="n">Println </span><span class="p">(</span><span class="n">sign </span> <span class="o">== </span> <span class="n">expectedHMAC </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">const </span> <span class="n">apiKey </span> <span class="o">= </span> <span class="s">"User1Key" </span> <span class="k">const </span> <span class="n">apiSecret </span> <span class="o">= </span> <span class="s">"User1Secret"</span> <span class="n">data </span> <span class="o">:= </span> <span class="p">[] </span><span class="kt">byte </span><span class="p">(</span><span class="s">"data" </span><span class="p">)</span> <span class="n">h </span> <span class="o">:= </span> <span class="n">hmac </span><span class="o">。</span><span class="n">新規</span><span class="p">(</span><span class="n">sha256 </span><span class="o">。</span><span class="n">新規</span><span class="p">、</span> <span class="p">[] </span><span class="kt">バイト</span><span class="p">(</span><span class="n">apiSecret </span><span class="p">))</span> <span class="n">h </span><span class="o">。</span><span class="n">書き込み</span><span class="p">(</span><span class="n">データ</span><span class="p">)</span> <span class="n">記号</span> <span class="o">:= </span> <span class="n">16進数</span><span class="o">。</span><span class="n">EncodeToString </span><span class="p">(</span><span class="n">H </span><span class="o">。</span><span class="n">和</span><span class="p">(</span><span class="no">ゼロ</span><span class="p">))</span> <span class="c">//ハッシュ値</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="c">// </span> <span class="n">サーバー</span><span class="c">に投げる</span><span class="n">Server </span><span class="p">(</span><span class="n">apiKey </span><span class="p">、</span> <span class="n">sign </span><span class="p">、</span> <span class="n">data </span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 |
076b55e7f7e12624b4569f162302f1e36c11fb3a9134889267748de14a84b996 本当 |
セマフォ
- goroutineが走る数を限定することができる
1 |
golang.org/x/sync/semaphoreを取得します |
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 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"context" </span> <span class="s">"fmt" </span> <span class="s">"time"</span> <span class="s">"golang.org/x/sync/semaphore" </span> <span class="p">)</span> <span class="c">//並行実行できるプロセス数を制限</span> <span class="k">var </span> <span class="n">s </span> <span class="o">* </span><span class="n">セマフォ</span><span class="o">。</span><span class="n">加重</span> <span class="o">= </span> <span class="n">セマフォ</span><span class="o">。</span><span class="n">NewWeighted </span><span class="p">(</span><span class="m">1 </span><span class="p">)</span> <span class="k">FUNCの</span> <span class="n">longProcess </span><span class="p">(</span><span class="n">CTX </span> <span class="n">コンテキスト</span><span class="o">。</span><span class="n">コンテキスト</span><span class="p">){ </span> <span class="c">//処理が並行している場合ロックをかけることができる</span> <span class="n">isAcquire </span> <span class="o">:= </span> <span class="n">S </span><span class="o">。</span><span class="n">次の</span><span class="k">場合は、</span><span class="n">TryAcquire </span><span class="p">(</span><span class="m">1 </span><span class="p">)を実行し</span> <span class="k">て</span> <span class="o">ください。</span><span class="n">isAcquire </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="k">return </span> <span class="p">}</span> <span class="k">延期</span> <span class="n">秒</span><span class="o">。</span><span class="n">リリース</span><span class="p">(</span><span class="m">1 </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">"Wait ..." </span><span class="p">)</span> <span class="n">時間</span><span class="o">。</span><span class="n">スリープ</span><span class="p">(</span><span class="m">1 </span> <span class="o">* </span> <span class="n">時間</span><span class="o">。</span><span class="n">秒</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="k">func </span> <span class="n">main </span><span class="p">()</span> <span class="p">{ </span> <span class="n">ctx </span> <span class="o">:= </span> <span class="n">context </span><span class="o">。</span><span class="n">TODO </span><span class="p">()</span> <span class="k">go </span> <span class="n">longProcess </span><span class="p">(</span><span class="n">ctx </span><span class="p">)</span> <span class="k">go </span> <span class="n">longProcess </span><span class="p">(</span><span class="n">ctx </span><span class="p">)</span> <span class="k">go </span> <span class="n">longProcess </span><span class="p">(</span><span class="n">ctx </span><span class="p">)</span> <span class="n">時間</span><span class="o">。</span><span class="n">スリープ</span><span class="p">(</span><span class="m">5 </span> <span class="o">* </span> <span class="n">時間</span><span class="o">。</span><span class="n">秒</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 |
ロックを取得できませんでした ロックを取得できませんでした 待つ... できた |
ini
- configファイルを必須
- サーバーの設定値など
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">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"github.com/go-ini/ini" </span> <span class="p">)</span> <span class="k">タイプ</span> <span class="n">ConfigList </span> <span class="k">struct </span><span class="p">{ </span> <span class="n">Port </span> <span class="kt">int </span> <span class="n">DbName </span> <span class="kt">string </span> <span class="n">SQLDriver </span> <span class="kt">string </span> <span class="p">}</span> <span class="k">var </span> <span class="n">Config </span> <span class="n">ConfigList</span> <span class="c">// main </span> <span class="k">関数</span> <span class="c">が呼ばれる前に設定を呼び込む</span><span class="k">func </span><span class="n">init </span><span class="p">(){ </span> <span class="n">cfg </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">ini </span><span class="o">。</span><span class="n">Load </span><span class="p">(</span><span class="s">"config.ini" </span><span class="p">)</span> <span class="n">Config </span> <span class="o">= </span> <span class="n">ConfigList </span><span class="p">{ </span> <span class="n">Port </span><span class="o">:</span> <span class="n">cfg </span><span class="o">。</span><span class="n">セクション</span><span class="p">(</span><span class="s">"web" </span><span class="p">)</span><span class="o">。</span><span class="n">キー</span><span class="p">(</span><span class="s">"ポート" </span><span class="p">)</span><span class="o">。</span><span class="n">MustInt </span><span class="p">()、</span> <span class="n">DbName </span><span class="o">:</span> <span class="n">cfg </span><span class="o">。</span><span class="n">セクション</span><span class="p">(</span><span class="s">"db" </span><span class="p">)</span><span class="o">。</span><span class="n">キー</span><span class="p">(</span><span class="s">"名前" </span><span class="p">)</span><span class="o">。</span><span class="n">MustString </span><span class="p">(</span><span class="s">"example.sql"</span><span class="p">)、</span> <span class="n">SQLDriver </span><span class="o">:</span> <span class="n">cfg </span><span class="o">。</span><span class="n">セクション</span><span class="p">(</span><span class="s">"db" </span><span class="p">)</span><span class="o">。</span><span class="n">キー</span><span class="p">(</span><span class="s">「ドライバー」</span><span class="p">)</span><span class="o">。</span><span class="n">文字列</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="n">fmt </span><span class="o">。</span><span class="n">Printf </span><span class="p">(</span><span class="s">"%T%v </span><span class="se">\ n </span><span class="s">" </span><span class="p">、</span> <span class="n">構成</span><span class="o">。</span><span class="n">ポート</span><span class="p">、</span> <span class="n">構成</span><span class="o">。</span><span class="n">ポート</span><span class="p">)</span> <span class="n">fmt </span><span class="o">。</span><span class="n">printf </span><span class="p">(</span><span class="s">"%のTの体積%</span><span class="se">\ n </span><span class="s">" </span><span class="p">、</span> <span class="n">構成</span><span class="o">。</span><span class="n">DBNAME </span><span class="p">、</span> <span class="n">コンフィグ</span><span class="o">。</span><span class="n">DBNAME </span><span class="p">)</span> <span class="n">FMT </span><span class="o">。</span><span class="n">printf </span><span class="p">(</span><span class="s">"%のTの体積%</span><span class="se">\ n </span><span class="s">" </span><span class="p">、</span> <span class="n">構成</span><span class="o">。</span><span class="n">SQLDriver </span><span class="p">、</span> <span class="n">コンフィグ</span><span class="o">。</span><span class="n">SQLDriver </span><span class="p">)</span> <span class="p">}</span> |
config.ini
1 2 3 4 5 6 |
<span class="nn">[ウェブ] </span> <span class="py">ポート</span> <span class="p">= </span> <span class="s">8080</span> <span class="nn">[db] </span> <span class="py">名前</span> <span class="p">= </span> <span class="s">stockdata.sql </span> <span class="py">ドライバー</span> <span class="p">= </span> <span class="s">sqlite3</span> |
出力
1 2 3 |
int 8080 文字列stockdata.sql 文字列sqlite3 |
教材
https://www.udemy.com/course/go-fintech/learn/lecture/12088980