変数
var
を使った変数宣言は関数の外でも使えるxi
などの短縮版の変数宣言は関数の中でしか使えない
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="s">"fmt"</span> <span class="k">var</span> <span class="p">(</span> <span class="n">i</span> <span class="kt">int</span> <span class="o">=</span> <span class="m">1</span> <span class="n">f64</span> <span class="kt">float64</span> <span class="o">=</span> <span class="m">1.2</span> <span class="n">s</span> <span class="kt">string</span> <span class="o">=</span> <span class="s">"test"</span> <span class="n">t</span><span class="p">,</span> <span class="n">f</span> <span class="kt">bool</span> <span class="o">=</span> <span class="no">true</span><span class="p">,</span> <span class="no">false</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">i</span><span class="p">,</span> <span class="n">f64</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span> <span class="n">xi</span> <span class="o">:=</span> <span class="m">666</span> <span class="n">xf64</span> <span class="o">:=</span> <span class="m">66.6</span> <span class="n">xs</span> <span class="o">:=</span> <span class="s">"aaa"</span> <span class="n">xt</span><span class="p">,</span> <span class="n">xf</span> <span class="o">:=</span> <span class="no">false</span><span class="p">,</span> <span class="no">true</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">xi</span><span class="p">,</span> <span class="n">xf64</span><span class="p">,</span> <span class="n">xs</span><span class="p">,</span> <span class="n">xt</span><span class="p">,</span> <span class="n">xf</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">""</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 4 |
yuta@DESKTOP-V36210S:/mnt/c/gostudy$ ./test 1 1.2 test true false 666 66.6 aaa false true yuta@DESKTOP-V36210S:/mnt/c/gostudy$ |
定数
const
で定数を指定する- ユーザー、パスワードなどは定数がいいのだと思yyk
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">const</span> <span class="n">Pi</span> <span class="o">=</span> <span class="m">3.14</span> <span class="k">const</span> <span class="p">(</span> <span class="n">username</span> <span class="o">=</span> <span class="s">"user"</span> <span class="n">password</span> <span class="o">=</span> <span class="s">"pass"</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">Pi</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 |
yuta@DESKTOP-V36210S:/mnt/c/gostudy$ ./test 3.14 user pass |
数値型の演算
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="s">"fmt"</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="s">"---数値型---"</span><span class="p">)</span> <span class="k">var</span> <span class="p">(</span> <span class="n">u8</span> <span class="kt">uint8</span> <span class="o">=</span> <span class="m">255</span> <span class="c">//32bitか64bit環境かでの環境依存</span> <span class="n">i8</span> <span class="kt">int8</span> <span class="o">=</span> <span class="m">127</span> <span class="c">//</span> <span class="n">f32</span> <span class="kt">float64</span> <span class="o">=</span> <span class="m">0.2</span> <span class="n">c64</span> <span class="kt">complex64</span> <span class="o">=</span> <span class="o">-</span><span class="m">5</span> <span class="o">+</span> <span class="m">12i</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">u8</span><span class="p">,</span> <span class="n">i8</span><span class="p">,</span> <span class="n">f32</span><span class="p">,</span> <span class="n">c64</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">"type=%T value=%v"</span><span class="p">,</span> <span class="n">u8</span><span class="p">,</span> <span class="n">u8</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">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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"1 + 1 ="</span><span class="p">,</span> <span class="m">1</span><span class="o">+</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">"10.0 / 3 ="</span><span class="p">,</span> <span class="m">10.0</span><span class="o">/</span><span class="m">3</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">"10 % 3 ="</span><span class="p">,</span> <span class="m">10</span><span class="o">%</span><span class="m">3</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">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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="m">1</span> <span class="o"><<</span> <span class="m">0</span><span class="p">)</span> <span class="c">//0001 → 0001</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="m">1</span> <span class="o"><<</span> <span class="m">1</span><span class="p">)</span> <span class="c">//0001 → 0010</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="m">1</span> <span class="o"><<</span> <span class="m">2</span><span class="p">)</span> <span class="c">//0001 → 0100</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="m">1</span> <span class="o"><<</span> <span class="m">3</span><span class="p">)</span> <span class="c">//0001 → 1000</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">()</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 7 8 9 10 11 12 13 |
---数値型--- 255 127 0.2 (-5+12i) type=uint8 value=255 ---演算--- 1 + 1 = 2 10.0 / 3 = 3.3333333333333335 10 % 3 = 1 ---シフト演算--- 1 2 4 8 |