ポインタ型
&n
でポインタのアドレスを指定*int
で指定したアドレスの中身を取得
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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">n</span> <span class="kt">int</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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="o">&</span><span class="n">n</span><span class="p">)</span> <span class="c">// pはポインタ型</span> <span class="k">var</span> <span class="n">p</span> <span class="o">*</span><span class="kt">int</span> <span class="o">=</span> <span class="o">&</span><span class="n">n</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="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="n">p</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 |
100 0xc000104030 0xc000104030 100 |
newとmake
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="c">// メモリのアドレスを確保している</span> <span class="k">var</span> <span class="n">p</span> <span class="o">*</span><span class="kt">int</span> <span class="o">=</span> <span class="nb">new</span><span class="p">(</span><span class="kt">int</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="p">)</span> <span class="c">// メモリのアドレスは確保していないのでnil</span> <span class="k">var</span> <span class="n">p2</span> <span class="o">*</span><span class="kt">int</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span> <span class="p">}</span> |
1 2 |
0xc0000120b8 <nil> |
struct
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">type</span> <span class="n">Vertex</span> <span class="k">struct</span><span class="p">{</span> <span class="n">X</span><span class="p">,</span> <span class="n">Y</span> <span class="kt">int</span> <span class="n">S</span> <span class="kt">string</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">// XとYを指定して数値を入れる</span> <span class="n">v</span> <span class="o">:=</span> <span class="n">Vertex</span><span class="p">{</span><span class="n">X</span><span class="o">:</span> <span class="m">1</span><span class="p">,</span> <span class="n">Y</span><span class="o">:</span><span class="m">2</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">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="n">v</span><span class="o">.</span><span class="n">Y</span><span class="p">)</span> <span class="n">v</span><span class="o">.</span><span class="n">X</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">v</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="n">v</span><span class="o">.</span><span class="n">Y</span><span class="p">)</span> <span class="c">// Xだけ入れるとYは0扱い</span> <span class="n">v2</span> <span class="o">:=</span> <span class="n">Vertex</span><span class="p">{</span><span class="n">X</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="n">v2</span><span class="p">)</span> <span class="c">// XやYを指定しない場合 文字列も含む</span> <span class="n">v3</span> <span class="o">:=</span> <span class="n">Vertex</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="s">"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">v3</span><span class="p">)</span> <span class="c">// なにも入れない場合はデフォルトの0か空文字が入る</span> <span class="n">v4</span> <span class="o">:=</span> <span class="n">Vertex</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">v4</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 5 6 |
{1 2 } 1 2 100 2 {1 0 } {1 2 test} {0 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 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">type</span> <span class="n">Vertex</span> <span class="k">struct</span><span class="p">{</span> <span class="c">//小文字にするとx.yを書き換えられる</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="kt">int</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="n">Vertex</span><span class="p">)</span> <span class="n">Area</span><span class="p">()</span> <span class="kt">int</span><span class="p">{</span> <span class="k">return</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">v</span> <span class="o">*</span><span class="n">Vertex</span><span class="p">)</span> <span class="n">Scale</span><span class="p">(</span><span class="n">i</span> <span class="kt">int</span><span class="p">){</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">x</span> <span class="o">*</span> <span class="n">i</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">y</span> <span class="o">*</span> <span class="n">i</span> <span class="p">}</span> <span class="k">func</span> <span class="n">New</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="kt">int</span><span class="p">)</span> <span class="o">*</span><span class="n">Vertex</span><span class="p">{</span> <span class="k">return</span> <span class="o">&</span><span class="n">Vertex</span><span class="p">{</span><span class="n">x</span><span class="p">,</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="n">v</span> <span class="o">:=</span> <span class="n">Vertex</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="n">v</span><span class="o">.</span><span class="n">Scale</span><span class="p">(</span><span class="m">10</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="o">.</span><span class="n">Area</span><span class="p">())</span> <span class="p">}</span> |
1 |
1200 |