map型
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="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="c">// 要素全部表示</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</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">m</span><span class="p">[</span><span class="s">"apple"</span><span class="p">])</span> <span class="c">// 要素追加</span> <span class="n">m</span><span class="p">[</span><span class="s">"banana"</span><span class="p">]</span> <span class="o">=</span> <span class="m">300</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="n">m</span><span class="p">[</span><span class="s">"new"</span><span class="p">]</span> <span class="o">=</span> <span class="m">500</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="c">// ないものを取り出そうとすると0になる</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="s">"nothing"</span><span class="p">])</span> <span class="c">// boolean型も返す</span> <span class="n">v</span><span class="p">,</span> <span class="n">ok</span> <span class="o">:=</span> <span class="n">m</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">v</span><span class="p">,</span> <span class="n">ok</span><span class="p">)</span> <span class="c">// boolean型も返す</span> <span class="n">v2</span><span class="p">,</span> <span class="n">ok2</span> <span class="o">:=</span> <span class="n">m</span><span class="p">[</span><span class="s">"nothing"</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">v2</span><span class="p">,</span> <span class="n">ok2</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 7 8 9 |
API server listening at: 127.0.0.1:4056 map[apple:100 banana:200] 100 map[apple:100 banana:300] map[apple:100 banana:300 new:500] 0 100 true 0 false Process exiting with code: 0 |
byte型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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">b</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">byte</span><span class="p">{</span><span class="m">72</span><span class="p">,</span> <span class="m">73</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">b</span><span class="p">)</span> <span class="c">// stringにキャスト ASCIIコード</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">b</span><span class="p">))</span> <span class="n">c</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">byte</span><span class="p">(</span><span class="s">"HI"</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">c</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">c</span><span class="p">))</span> <span class="p">}</span> |
1 2 3 4 |
[72 73] HI [72 73] HI |
関数
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="c">// 関数</span> <span class="k">func</span> <span class="n">add</span><span class="p">(</span><span class="n">x</span> <span class="kt">int</span><span class="p">,</span> <span class="n">y</span> <span class="kt">int</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">"add function"</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">x</span> <span class="o">+</span> <span class="n">y</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">add</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">20</span><span class="p">)</span> <span class="p">}</span> |
1 2 |
add function 30 |
複数の戻り値をInt型で返す
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="c">// 返り値を右にかく</span> <span class="k">func</span> <span class="n">add</span><span class="p">(</span><span class="n">x</span> <span class="kt">int</span><span class="p">,</span> <span class="n">y</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</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">r1</span><span class="p">,</span> <span class="n">r2</span> <span class="o">:=</span> <span class="n">add</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">20</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">r1</span><span class="p">,</span> <span class="n">r2</span><span class="p">)</span> <span class="p">}</span> |
1 |
30 -10 |