wslにセットアップ
インストール
1 |
<span class="nb">sudo </span>apt-get <span class="nt">-y</span> <span class="nb">install </span>golang-go |
環境変数
1 |
vim ~/.bashrc |
1 2 3 4 |
# goenv export GOENV_ROOT=$HOME/.goenv export PATH=$GOENV_ROOT/bin:$PATH eval "$(goenv init -)" |
goenvインストール
1 |
git clone https://github.com/syndbg/goenv.git ~/.goenv |
1 2 3 4 5 |
exec $SHELL source ~/.bashrc goenv install -l goenv install 1.14.4 goenv global 1.14.4 |
バージョン確認
1 2 3 |
yuta@DESKTOP-V36210S:/mnt/c/Users/yuta$ goenv versions system * 1.14.4 (set by /home/yuta/.goenv/version) |
文字列置換
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"fmt"</span> <span class="s">"strings"</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="s">"Hello world"</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">"Hello"</span> <span class="o">+</span> <span class="s">" world"</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="s">"Hello world"</span><span class="p">[</span><span class="m">0</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="n">s</span> <span class="kt">string</span> <span class="o">=</span> <span class="s">"Hello world"</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">strings</span><span class="o">.</span><span class="n">Replace</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s">"H"</span><span class="p">,</span> <span class="s">"X"</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="n">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="n">strings</span><span class="o">.</span><span class="n">Contains</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s">"world"</span><span class="p">))</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 7 |
Hello world Hello world H ---置き換え--- Xello world Hello world true |
論理値
1 2 3 4 5 6 7 8 9 |
<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">t</span><span class="p">,</span> <span class="n">f</span> <span class="o">:=</span> <span class="no">true</span><span class="p">,</span> <span class="no">false</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"%T %v %t</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">t</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">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"%T %v %t</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span> <span class="p">}</span> |
出力結果
1 2 3 |
bool true false bool false true Process exiting with code: 0 |
- %tで型を強制する
- 数値を入れると怒られる
1 2 3 4 5 6 7 8 9 |
<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">t</span><span class="p">,</span> <span class="n">f</span> <span class="o">:=</span> <span class="no">true</span><span class="p">,</span> <span class="no">false</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"%T %v %t</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">t</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">Printf</span><span class="p">(</span><span class="s">"%T %v %t</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="m">0</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 |
bool true %!t(int=1) bool false %!t(int=0) |
論理演算子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<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="c">// and</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="no">true</span> <span class="o">&&</span> <span class="no">true</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="no">true</span> <span class="o">&&</span> <span class="no">false</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="no">false</span> <span class="o">&&</span> <span class="no">false</span><span class="p">)</span> <span class="c">// or</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="no">true</span> <span class="o">||</span> <span class="no">true</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="no">true</span> <span class="o">||</span> <span class="no">false</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="no">false</span> <span class="o">||</span> <span class="no">false</span><span class="p">)</span> <span class="c">// not</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="o">!</span><span class="no">true</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="o">!</span><span class="no">false</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 7 8 |
true false false true true false false true |
型変換
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package main import ( "fmt" "strconv" ) func main() { // int → float var x int = 1 xx := float64(x) fmt.Printf("%T %v %f\n", xx, xx, xx) // float64 → int var y float64 = 1.2 yy := int(y) fmt.Printf("%T %v %d\n", yy, yy, yy) // string コンバートする必要があるASCII to Integer var s string = "14" // 「_」の部分にエラーが入る errとかにしてif err みたいな感じでエラーハンドリングする i, _ := strconv.Atoi(s) fmt.Printf("%T %v", i, i) } |
出力結果
1 2 3 4 |
float64 1 1.000000 int 1 1 int 14 Process exi |
配列
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="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="k">var</span> <span class="n">a</span> <span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="m">0</span><span class="p">]</span> <span class="o">=</span> <span class="m">100</span> <span class="n">a</span><span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="o">=</span> <span class="m">200</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="k">var</span> <span class="n">b</span> <span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="kt">int</span> <span class="o">=</span> <span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="m">100</span><span class="p">,</span> <span class="m">200</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="p">}</span> |
1 2 |
<span class="o">[</span>100 200] <span class="o">[</span>100 200] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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="k">var</span> <span class="n">a</span> <span class="p">[</span><span class="m">2</span><span class="p">]</span><span class="kt">int</span> <span class="n">a</span><span class="p">[</span><span class="m">0</span><span class="p">]</span> <span class="o">=</span> <span class="m">100</span> <span class="n">a</span><span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="o">=</span> <span class="m">200</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c">/* var b [2]int = [2]int{100, 200} fmt.Println(b) */</span> <span class="c">// 要素数を増やす場合、配列ではなくスライス</span> <span class="k">var</span> <span class="n">b</span> <span class="p">[]</span><span class="kt">int</span> <span class="o">=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">100</span><span class="p">,</span> <span class="m">200</span><span class="p">}</span> <span class="n">b</span> <span class="o">=</span> <span class="nb">append</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="m">300</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="p">}</span> |
1 2 3 |
<span class="o">[</span>100 200] <span class="o">[</span>100 200 300] Process exiti |
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 |
<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">n</span> <span class="o">:=</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span> <span class="m">6</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">n</span><span class="p">)</span> <span class="c">// 2番目</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">n</span><span class="p">[</span><span class="m">2</span><span class="p">])</span> <span class="c">// 2-4</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">n</span><span class="p">[</span><span class="m">2</span><span class="o">:</span><span class="m">4</span><span class="p">])</span> <span class="c">// 0-2</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">n</span><span class="p">[</span><span class="o">:</span><span class="m">2</span><span class="p">])</span> <span class="c">// 2-最後の要素</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">n</span><span class="p">[</span><span class="m">2</span><span class="o">:</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">n</span><span class="p">[</span><span class="o">:</span><span class="p">])</span> <span class="c">// 2番目のみ書き換え</span> <span class="n">n</span><span class="p">[</span><span class="m">2</span><span class="p">]</span> <span class="o">=</span> <span class="m">100</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="c">// 入れ子</span> <span class="k">var</span> <span class="n">board</span> <span class="o">=</span> <span class="p">[][]</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="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">},</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</span><span class="p">},</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="m">6</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">8</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">board</span><span class="p">)</span> <span class="c">// 追加</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">append</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="m">100</span><span class="p">,</span> <span class="m">200</span><span class="p">,</span> <span class="m">300</span><span class="p">,</span> <span class="m">400</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">n</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 7 8 9 |
[1 2 3 4 5 6] 3 [3 4] [1 2] [3 4 5 6] [1 2 3 4 5 6] [1 2 100 4 5 6] [[0 1 2] [3 4 5] [6 7 8]] [1 2 100 4 5 6 100 200 300 400] |
make 配列作る
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">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="c">// 長さが3, 容量が5(メモリが確保する部分)</span> <span class="n">n</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">int</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">5</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">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span> <span class="c">// 要素を2つ追加</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">append</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">0</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">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span> <span class="c">// 要素を2つ追加</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">append</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</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">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span> <span class="c">// 次の書き方だと長さ、容量両方3になる</span> <span class="n">a</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">int</span><span class="p">,</span> <span class="m">3</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">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">a</span><span class="p">)</span> <span class="c">// bだとメモリ確保する、cだとメモリ確保しない書き方</span> <span class="n">b</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">int</span><span class="p">,</span> <span class="m">0</span><span class="p">)</span> <span class="k">var</span> <span class="n">c</span> <span class="p">[]</span><span class="kt">int</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">b</span><span class="p">),</span> <span class="n">b</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">"len=%d cap=%d value=%v</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">cap</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="n">c</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 4 5 6 |
<span class="nv">len</span><span class="o">=</span>3 <span class="nv">cap</span><span class="o">=</span>5 <span class="nv">value</span><span class="o">=[</span>0 0 0] <span class="nv">len</span><span class="o">=</span>5 <span class="nv">cap</span><span class="o">=</span>5 <span class="nv">value</span><span class="o">=[</span>0 0 0 0 0] <span class="nv">len</span><span class="o">=</span>10 <span class="nv">cap</span><span class="o">=</span>10 <span class="nv">value</span><span class="o">=[</span>0 0 0 0 0 1 2 3 4 5] <span class="nv">len</span><span class="o">=</span>3 <span class="nv">cap</span><span class="o">=</span>3 <span class="nv">value</span><span class="o">=[</span>0 0 0] <span class="nv">len</span><span class="o">=</span>0 <span class="nv">cap</span><span class="o">=</span>0 <span class="nv">value</span><span class="o">=[]</span> <span class="nv">len</span><span class="o">=</span>0 <span class="nv">cap</span><span class="o">=</span>0 <span class="nv">value</span><span class="o">=[]</span> |