no-struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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">MyInt</span> <span class="kt">int</span> <span class="k">func</span> <span class="p">(</span><span class="n">i</span> <span class="n">MyInt</span><span class="p">)</span> <span class="n">Double</span><span class="p">()</span> <span class="kt">int</span><span class="p">{</span> <span class="c">// integerにキャストする</span> <span class="k">return</span> <span class="kt">int</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="m">2</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">myInt</span> <span class="o">:=</span> <span class="n">MyInt</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">myInt</span><span class="o">.</span><span class="n">Double</span><span class="p">())</span> <span class="p">}</span> |
1 |
20 |
インターフェース
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 |
<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">Human</span> <span class="k">interface</span> <span class="p">{</span> <span class="n">Say</span><span class="p">()</span> <span class="kt">string</span> <span class="p">}</span> <span class="k">type</span> <span class="n">Person</span> <span class="k">struct</span><span class="p">{</span> <span class="n">Name</span> <span class="kt">string</span> <span class="p">}</span> <span class="k">func</span> <span class="p">(</span><span class="n">p</span> <span class="o">*</span><span class="n">Person</span><span class="p">)</span> <span class="n">Say</span><span class="p">()</span> <span class="kt">string</span><span class="p">{</span> <span class="n">p</span><span class="o">.</span><span class="n">Name</span> <span class="o">=</span> <span class="s">"Mr."</span> <span class="o">+</span> <span class="n">p</span><span class="o">.</span><span class="n">Name</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">Name</span><span class="p">)</span> <span class="k">return</span> <span class="n">p</span><span class="o">.</span><span class="n">Name</span> <span class="p">}</span> <span class="c">// Humanインターフェースを受け付ける</span> <span class="k">func</span> <span class="n">DriveCar</span><span class="p">(</span><span class="n">human</span> <span class="n">Human</span><span class="p">){</span> <span class="k">if</span> <span class="n">human</span><span class="o">.</span><span class="n">Say</span><span class="p">()</span> <span class="o">==</span> <span class="s">"Mr.Mike"</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="p">}</span><span class="k">else</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">"Get Out"</span><span class="p">)</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="k">var</span> <span class="n">mike</span> <span class="n">Human</span> <span class="o">=</span> <span class="o">&</span><span class="n">Person</span><span class="p">{</span><span class="s">"Mike"</span><span class="p">}</span> <span class="k">var</span> <span class="n">x</span> <span class="n">Human</span> <span class="o">=</span> <span class="o">&</span><span class="n">Person</span><span class="p">{</span><span class="s">"X"</span><span class="p">}</span> <span class="n">DriveCar</span><span class="p">(</span><span class="n">mike</span><span class="p">)</span> <span class="n">DriveCar</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">}</span> |
1 2 3 4 |
Mr.Mike Run Mr.X Get Out |