時間関数
- RFC3339がよく使われるPostgresqlなどで
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt" </span> <span class="s">"time" </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">t </span> <span class="o">:= </span> <span class="n">time </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="n">t </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="o">。</span><span class="n">フォーマット</span><span class="p">(</span><span class="n">時間</span><span class="o">。</span><span class="n">RFC3339 </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="o">。</span><span class="n">年</span><span class="p">()、</span> <span class="n">T </span><span class="o">。</span><span class="n">月</span><span class="p">()、</span> <span class="n">T </span><span class="o">。</span><span class="n">デイ</span><span class="p">()、</span> <span class="n">T </span><span class="o">。</span><span class="n">アワー</span><span class="p">()、</span> <span class="n">T </span><span class="o">。</span><span class="n">ミニッツ</span><span class="p">()、</span> <span class="n">T </span><span class="o">。</span><span class="n">セカンド</span><span class="p">())</span> <span class="p">}</span> |
1 2 3 |
2020-07-05 13:33:12.5673789 +0900 JST m = + 0.002000201 2020-07-05T13:33:12 + 09:00 2020年7月5日13 33 12 |
正規表現
- WebサーバのバックエンドでURLによって処理を検出する場合などに使う
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">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt" </span> <span class="s">"regexp" </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">//プログラム内で1回だけ</span> <span class="c">とかならまとめて書く// aaaeなどがヒットする</span> <span class="n">match </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">regexp </span><span class="o">。</span><span class="n">MatchString </span><span class="p">(</span><span class="s">"a([az] +)e" </span><span class="p">、</span> <span class="s">"apple" </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="c">//何度も使いまわす場合、MustCompileで正規表現のみ宣言しておく</span> <span class="n">r </span> <span class="o">:= </span> <span class="n">regexp </span><span class="o">。</span><span class="n">MustCompile </span><span class="p">(</span><span class="s">"a([az] +)e" </span><span class="p">)</span> <span class="n">ms </span> <span class="o">:= </span> <span class="n">r </span><span class="o">。</span><span class="n">MatchString </span><span class="p">(</span><span class="s">"apple" </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="c">// s:= "/ view / test"というようなURLをひっかけたい</span> <span class="n">r2 </span> <span class="o">:= </span> <span class="n">regexp </span><span class="o">。</span><span class="n">MustCompile </span><span class="p">(</span><span class="s">"^ /(edit | save | view)/([a-zA-Z0-9] +)$" </span><span class="p">)</span> <span class="n">fs </span> <span class="o">:= </span> <span class="n">r2 </span><span class="o">。</span><span class="n">FindString </span><span class="p">(</span><span class="s">"/ view / test" </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">fs </span><span class="p">)</span> <span class="c">//一部の文字だけ</span> <span class="n">取り出し</span> <span class="c">たい場合</span><span class="n">fss </span><span class="o">:= </span> <span class="n">r2 </span><span class="o">。</span><span class="n">FindStringSubmatch </span><span class="p">(</span><span class="s">"/ view / test" </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">fss </span><span class="p">、</span> <span class="n">fss </span><span class="p">[ </span><span class="m">0 </span><span class="p">]、</span> <span class="n">fss </span><span class="p">[ </span><span class="m">1 </span><span class="p">]、</span> <span class="n">fss </span><span class="p">[</span><span class="m">2 </span><span class="p">])</span> <span class="p">}</span> |
1 2 3 4 |
本当 本当 / view / test [/ view / testビューテスト] / view / testビューテスト |
ソート
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="s">"sort" </span> <span class="p">)</span> <span class="k">FUNC </span> <span class="n">メイン</span><span class="p">()</span> <span class="p">{ </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="m">5 </span><span class="p">、</span> <span class="m">3 </span><span class="p">、</span> <span class="m">2 </span><span class="p">、</span> <span class="m">8 </span><span class="p">、</span> <span class="m">7 </span><span class="p">} </span> <span class="n">S </span> <span class="o">:= </span> <span class="p">[] </span><span class="kt">ストリング</span><span class="p">{ </span><span class="s">"D" </span><span class="p">、</span> <span class="s">"" </span><span class="p">、</span> <span class="s">"F" </span><span class="p">} </span> <span class="n">P </span> <span class="o">:= </span> <span class="p">[] </span><span class="k">構造体</span> <span class="p">{ </span> <span class="n">名前</span> <span class="kt">ストリング</span> <span class="n">年齢</span> <span class="kt">int </span> <span class="p">} { </span> <span class="p">{ </span><span class="s">"Nancy" </span><span class="p">、</span> <span class="m">20 </span><span class="p">}、</span> <span class="p">{ </span><span class="s">"Vera" </span><span class="p">、</span> <span class="m">30 </span><span class="p">}、</span> <span class="p">{ </span><span class="s">"Mike" </span><span class="p">、</span> <span class="m">40 </span><span class="p">}、</span> <span class="p">{</span><span class="s">"Bob" </span><span class="p">、</span> <span class="m">50 </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">i </span><span class="p">、</span> <span class="n">s </span><span class="p">、</span> <span class="n">p </span><span class="p">)</span> <span class="n">ソート</span><span class="o">。</span><span class="n">Ints </span><span class="p">(</span><span class="n">i </span><span class="p">)</span> <span class="n">ソート</span><span class="o">。</span><span class="n">Strings </span><span class="p">(</span><span class="n">s </span><span class="p">)</span> <span class="c">//名前順</span> <span class="n">ソート</span><span class="o">。</span><span class="n">スライス</span><span class="p">(</span><span class="n">P </span><span class="p">、</span> <span class="k">FUNC </span><span class="p">(</span><span class="n">I </span><span class="p">、</span> <span class="n">J </span> <span class="kt">INT </span><span class="p">)</span> <span class="kt">BOOL </span> <span class="p">{ </span> <span class="k">リターン</span> <span class="n">P </span><span class="p">[ </span><span class="n">I </span><span class="p">] </span><span class="o">。</span><span class="n">名</span> <span class="o">< </span> <span class="n">P </span><span class="p">[ </span><span class="n">J </span><span class="p">] </span><span class="o">。</span><span class="n">名</span> <span class="p">})</span> <span class="c">//年齢順</span> <span class="n">ソート</span><span class="o">。</span><span class="n">スライス</span><span class="p">(</span><span class="n">P </span><span class="p">、</span> <span class="k">FUNC </span><span class="p">(</span><span class="n">I </span><span class="p">、</span> <span class="n">J </span> <span class="kt">INT </span><span class="p">)</span> <span class="kt">BOOL </span> <span class="p">{ </span> <span class="k">戻り</span> <span class="n">P </span><span class="p">[ </span><span class="n">I </span><span class="p">] </span><span class="o">。</span><span class="n">年齢</span> <span class="o">< </span> <span class="n">P </span><span class="p">[ </span><span class="n">J </span><span class="p">] </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="n">i </span><span class="p">、</span> <span class="n">s </span><span class="p">、</span> <span class="n">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 16 17 18 19 20 21 22 23 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="s">「fmt」を</span><span class="k">インポート</span> <span class="c">//自動的に連番を振ってくれるiotaを入れてあげなくても自動的にインクリメントされる</span> <span class="k">const </span> <span class="p">(</span> <span class="n">c1 </span> <span class="o">= </span> <span class="no">iota </span> <span class="n">c2 </span> <span class="n">c3 </span> <span class="p">)</span> <span class="k">const </span> <span class="p">(</span> <span class="c">// 0を使わない場合「_」</span> <span class="n">_ </span> <span class="o">= </span> <span class="no">iota </span> <span class="n">KB </span> <span class="kt">int </span> <span class="o">= </span> <span class="m">1 </span> <span class="o"><< </span> <span class="p">(</span><span class="m">10 </span> <span class="o">* </span> <span class="no">iota </span><span class="p">)</span> <span class="n">MB </span> <span class="n">GB </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">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="n">c2 </span><span class="p">、</span> <span class="n">c3 </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">KB </span><span class="p">、</span> <span class="n">MB </span><span class="p">、</span> <span class="n">GB </span><span class="p">)</span> <span class="p">}</span> |
1 2 |
0 1 2 1024 1048576 1073741824 |
環境
- Webアプリなどでユーザーリクエストの処理が長い場合の続き設定などに使う
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 |
<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="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="n">CH </span> <span class="k">ちゃん</span> <span class="kt">列</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="s">"run" </span><span class="p">)</span> <span class="c">// 2秒の</span> <span class="n">時間</span><span class="o">。</span><span class="n">スリープ</span><span class="p">(</span><span class="m">2 </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">"finish" </span><span class="p">)</span> <span class="n">ch </span> <span class="o"><- </span> <span class="s">"result" </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">ch </span> <span class="o">:= </span> <span class="nb">make </span><span class="p">(</span><span class="k">chan </span> <span class="kt">string </span><span class="p">)</span> <span class="c">// context timeoutを付け加えられる</span> <span class="n">ctx </span> <span class="o">:= </span> <span class="n">context </span><span class="o">。</span><span class="n">背景</span><span class="p">()</span> <span class="c">// 1秒</span> <span class="n">ctx </span><span class="p">、</span> <span class="n">キャンセル</span> <span class="o">:= </span> <span class="n">コンテキスト</span><span class="o">。</span><span class="n">WithTimeout </span><span class="p">(</span><span class="n">CTX </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="k">延期</span> <span class="n">、キャンセルは</span><span class="p">()</span> <span class="k">に行く</span> <span class="n">longProcessを</span><span class="p">(</span><span class="n">CTX </span><span class="p">、</span> <span class="n">CH </span><span class="p">)</span> <span class="k">用</span> <span class="p">{ </span> <span class="k">選択</span> <span class="p">{ </span> <span class="k">場合を</span> <span class="o">< - </span><span class="n">CTX </span><span class="o">。</span><span class="n">完了</span><span class="p">()</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">CTX </span><span class="o">。</span><span class="n">のErr </span><span class="p">())</span> <span class="k">リターン</span> <span class="k">ケース</span> <span class="o">< - </span><span class="n">CH </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">"success" </span><span class="p">)</span> <span class="k">return </span> <span class="p">} </span> <span class="p">} </span> <span class="p">}</span> |
1 2 |
走る コンテキストの期限を超えました |
ioutil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"fmt" </span> <span class="s">"io / ioutil" </span> <span class="s">"log" </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">//パケットやファイルを読み込む場合ioutilを使う</span> <span class="c">// osとは役割が違う</span> <span class="n">content </span><span class="p">、</span> <span class="n">err </span> <span class="o">:= </span> <span class="n">ioutil </span><span class="o">。</span><span class="n">ReadFile </span><span class="p">(</span><span class="s">"main.go" </span><span class="p">)</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">致命的</span><span class="p">(</span><span class="n">エラー</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="kt">文字列</span><span class="p">(</span><span class="n">コンテンツ</span><span class="p">))</span> <span class="n">err </span> <span class="o">:= </span> <span class="n">ioutilの</span><span class="k">場合</span><span class="o">。</span><span class="n">WriteFile </span><span class="p">(</span><span class="s">"ioutil_temp.go" </span><span class="p">、</span> <span class="n">content </span><span class="p">、</span> <span class="m">0666 </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">log </span><span class="o">。</span><span class="n">Fatalln </span><span class="p">(</span><span class="n">エラー</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 16 17 18 19 20 21 22 23 |
127.0.0.1:17064でリッスンするAPIサーバー パッケージメイン インポート( 「fmt」 「io / ioutil」 "ログ" ) func main(){ //パケットやファイルをプロキシ場合ioutilを使う // osとは役割が違う 内容、エラー:= ioutil.ReadFile( "main.go") if err!= nil { log.Fatal(err) } fmt.Println(文字列(コンテンツ)) if err:= ioutil.WriteFile( "ioutil_temp.go"、content、0666); err!= nil { log.Fatalln(err) } } コードで終了するプロセス:0 |
ファイルが生成される
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"bytes" </span> <span class="s">"fmt" </span> <span class="s">"io / ioutil" </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">r </span> <span class="o">:= </span> <span class="n">bytes </span><span class="o">。</span><span class="n">NewBuffer </span><span class="p">([] </span><span class="kt">byte </span><span class="p">(</span><span class="s">"abc" </span><span class="p">))</span> <span class="c">// </span> <span class="n">バッファ</span><span class="c">はReadAllで</span><span class="n">コンテンツ</span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">ioutil </span><span class="o">。</span><span class="n">ReadAll </span><span class="p">(</span><span class="n">r </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">content </span><span class="p">))</span> <span class="p">}</span> |
1 |
ABC |
ネットワーク系パッケージ
- URLを読む
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">「fmt」</span> <span class="s">「io / ioutil」</span> <span class="s">「net / http」</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">resp </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">http </span><span class="o">。</span><span class="n">Get </span><span class="p">(</span><span class="s">"http://example.com" </span><span class="p">)</span> <span class="k">defer </span> <span class="n">resp </span><span class="o">。</span><span class="n">ボディ</span><span class="o">。</span><span class="n">close </span><span class="p">()</span> <span class="n">body </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">ioutil </span><span class="o">。</span><span class="n">READALL </span><span class="p">(</span><span class="n">RESP </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="kt">string </span><span class="p">(</span><span class="n">body </span><span class="p">))</span> <span class="p">}</span> |
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 41 42 43 44 45 46 47 48 49 50 51 52 |
127.0.0.1:37622でリッスンするAPIサーバー <!doctype html> <html> <ヘッド> <title>ドメインの例</ title> <meta charset = "utf-8" /> <meta http-equiv = "Content-type" content = "text / html; charset = utf-8" /> <meta name = "viewport" content = "width = device-width、 initial-scale = 1" /> <style type = "text / css"> 体 { background-color:#f0f0f2; マージン:0; パディング:0; font-family:-apple-system、system-ui、BlinkMacSystemFont、 「Segoe UI」、「Open Sans」、「Helvetica Neue」、Helvetica、Arial、sans-serif; } div { 幅:600px; マージン:5em auto; パディング:2em; background-color:#fdfdff; border-radius:0.5em; ボックスシャドウ:2px 3px 7px 2px rgba(0,0,0,0.02); } a:link、a:visited { 色:#38488f; text-decoration:なし。 } @media(最大幅:700px){ div { マージン:0自動; 幅:自動; } } </ style> </ head> <ボディ> <div> <h1>サンプルドメイン</ h1> <p>このドメインは、ドキュメントの例示的な例で使用するためのものです。 あなたはこれを使うかもしれません 事前の調整や許可を求めることなく、文学の領域。</ p> <p> <a href="https://www.iana.org/domains/example">詳細情報... </a> </ p> </ div> </ body> </ html> コードで終了するプロセス:0 |
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">"io / ioutil" </span> <span class="s">"net / http" </span> <span class="s">"net / url" </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">// URLが正しいものかチェック</span> <span class="n">ベース</span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">url </span><span class="o">。</span><span class="n">Parse </span><span class="p">(</span><span class="s">"http://example.com" </span><span class="p">)</span> <span class="c">// example.comのあとのURLをつけることができる</span> <span class="n">参照</span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">url </span><span class="o">。</span><span class="n">解析</span><span class="p">(</span><span class="s">"/ test?a = 1&n = 2" </span><span class="p">)</span> <span class="n">エンドポイント</span> <span class="o">:= </span> <span class="n">ベース</span><span class="o">。</span><span class="n">ResolveReference </span><span class="p">(</span><span class="n">リファレンス</span><span class="p">)</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="n">エンドポイント</span><span class="p">)</span> <span class="n">req </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">http </span><span class="o">。</span><span class="n">NewRequest </span><span class="p">(</span><span class="s">"GET" </span><span class="p">、</span> <span class="n">endpoint </span><span class="p">、</span> <span class="no">nil </span><span class="p">)</span> <span class="c">// </span> <span class="n">ヘッダーをつける</span><span class="c">ことができる</span><span class="n">req </span><span class="o">。</span><span class="n">ヘッダー</span><span class="o">。</span><span class="n">Add </span><span class="p">(</span><span class="s">"If-None-Match" </span><span class="p">、</span> <span class="s">"W / xxxx" </span><span class="p">)</span> <span class="n">q </span> <span class="o">:= </span> <span class="n">req </span><span class="o">。</span><span class="n">URL </span><span class="o">。</span><span class="n">クエリ</span><span class="p">()</span> <span class="n">q </span><span class="o">。</span><span class="p">(</span><span class="s">"c" </span><span class="p">、</span><span class="s">"3&%" </span><span class="p">)</span><span class="n">fmt </span><span class="n">を追加し</span><span class="o">ます。</span><span class="n">Println </span><span class="p">(</span><span class="n">q </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">Q </span><span class="o">。</span><span class="n">エンコード</span><span class="p">())</span><span class="n">のreq </span><span class="o">。</span><span class="n">URL </span><span class="o">。</span> <span class="n">RawQuery </span> <span class="o">= </span> <span class="n">q </span><span class="o">。</span><span class="n">エンコード</span><span class="p">()</span> <span class="k">var </span> <span class="n">client </span> <span class="o">* </span><span class="n">http </span><span class="o">。</span><span class="n">クライアント</span> <span class="o">= </span> <span class="o">&</span><span class="n">http </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">クライアント</span><span class="o">。</span><span class="n">ド</span><span class="p">(</span><span class="n">REQ </span><span class="p">)</span> <span class="n">ボディ</span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">ioutil </span><span class="o">。</span><span class="n">READALL </span><span class="p">(</span><span class="n">RESP </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="kt">string </span><span class="p">(</span><span class="n">body </span><span class="p">))</span> <span class="p">}</span> |
1 2 3 4 5 6 7 |
127.0.0.1:36071でリッスンするAPIサーバー http://example.com/test?a=1&n=2 map [a:[1] c:[3&%] n:[2]] a = 1&c = 3%26%25&n = 2 <!doctype html> <html> <ヘッド> |
アンマーシャル
- jsonデータをネットワークから受け取る場合などに利用
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">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"encoding / json" </span> <span class="s">"fmt" </span> <span class="p">)</span> <span class="k">タイプ</span> <span class="n">Person </span> <span class="k">struct </span> <span class="p">{ </span> <span class="c">// ``で</span> <span class="n">囲ってあげると変換してくれる</span><span class="n">Name </span> <span class="kt">string </span> <span class="s">`json:" name "` </span> <span class="n">Age </span> <span class="kt">int </span> <span class="s">`json:" age "` </span> <span class="n">NickNames </span> <span class="p">[] </span><span class="kt">string </span> <span class="s">`json:" nicknames "` </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">b </span> <span class="o">:= </span> <span class="p">[] </span><span class="kt">byte </span><span class="p">(</span><span class="s">`{" name ":" mike "、" age ":20、" nicknames " :[" a "、" b "、" c "]}` </span><span class="p">)</span> <span class="k">var </span> <span class="n">p </span> <span class="n">Person </span> <span class="c">//ネットワークから入ってきたjsonをStructに自動的に入れてくれる</span> <span class="k">if </span> <span class="n">err </span> <span class="o">:= </span> <span class="n">json </span><span class="o">。</span><span class="n">非整列化</span><span class="p">(</span><span class="n">b </span><span class="p">、</span> <span class="o">&</span><span class="n">p </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="n">fmt </span><span class="o">。</span><span class="n">println </span><span class="p">(</span><span class="n">P </span><span class="o">。</span><span class="n">名前</span><span class="p">、</span> <span class="n">P </span><span class="o">。</span> <span class="n">p </span><span class="o">。</span><span class="n">ニックネーム</span><span class="p">)</span> <span class="c">//再度変換をかける</span> <span class="n">v </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">json </span><span class="o">。</span><span class="n">元帥</span><span class="p">(</span><span class="n">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="kt">string </span><span class="p">(</span><span class="n">v </span><span class="p">))</span> <span class="p">}</span> |
1 2 |
マイク20 [abc] {"名前": "マイク"、 "年齢":20、 "ニックネーム":["a"、 "b"、 "c"]} |