Package
- 1階層
1 2 3 4 |
awesomeProject/ ├── main.go └── mylib └── math.go |
main.go
1 2 3 4 5 6 7 8 9 10 11 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"awesomeProject/mylib"</span> <span class="s">"fmt"</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">s</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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">mylib</span><span class="o">.</span><span class="n">Average</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">}</span> |
math.go
1 2 3 4 5 6 7 8 9 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">func</span> <span class="n">Average</span><span class="p">(</span><span class="n">s</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="n">total</span> <span class="o">:=</span> <span class="m">0</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">i</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">s</span> <span class="p">{</span> <span class="n">total</span> <span class="o">+=</span> <span class="n">i</span> <span class="p">}</span> <span class="k">return</span> <span class="kt">int</span><span class="p">(</span><span class="n">total</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">}</span> |
結果
1 |
3 |
- 2階層
階層
1 2 3 4 5 6 7 |
awesomeProject/ ├── main.go └── mylib ├── human.go ├── math.go └── under └── sub.go |
main.go
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="p">(</span> <span class="s">"awesomeProject/mylib"</span> <span class="s">"awesomeProject/mylib/under"</span> <span class="s">"fmt"</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">s</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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">mylib</span><span class="o">.</span><span class="n">Average</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="n">mylib</span><span class="o">.</span><span class="n">Say</span><span class="p">()</span> <span class="n">under</span><span class="o">.</span><span class="n">Hello</span><span class="p">()</span> <span class="p">}</span> |
math.go
1 2 3 4 5 6 7 8 9 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">func</span> <span class="n">Average</span><span class="p">(</span><span class="n">s</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="n">total</span> <span class="o">:=</span> <span class="m">0</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">i</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">s</span> <span class="p">{</span> <span class="n">total</span> <span class="o">+=</span> <span class="n">i</span> <span class="p">}</span> <span class="k">return</span> <span class="kt">int</span><span class="p">(</span><span class="n">total</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">}</span> |
human.go
1 2 3 4 5 6 7 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">func</span> <span class="n">Say</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">"Human!"</span><span class="p">)</span> <span class="p">}</span> |
sub.go
1 2 3 4 5 6 7 |
<span class="k">package</span> <span class="n">under</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">func</span> <span class="n">Hello</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!"</span><span class="p">)</span> <span class="p">}</span> |
出力
1 2 3 |
3 Human! Hello! |
Public,Private
- 外部から利用する場合には
Public
のように頭は大文字
main.go
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="p">(</span> <span class="s">"awesomeProject/mylib"</span> <span class="s">"awesomeProject/mylib/under"</span> <span class="s">"fmt"</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">s</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="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="n">mylib</span><span class="o">.</span><span class="n">Average</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="n">mylib</span><span class="o">.</span><span class="n">Say</span><span class="p">()</span> <span class="n">under</span><span class="o">.</span><span class="n">Hello</span><span class="p">()</span> <span class="n">person</span> <span class="o">:=</span> <span class="n">mylib</span><span class="o">.</span><span class="n">Person</span><span class="p">{</span><span class="n">Name</span><span class="o">:</span> <span class="s">"Mike"</span><span class="p">,</span> <span class="n">Age</span><span class="o">:</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">person</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">mylib</span><span class="o">.</span><span class="n">Public</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">mylib</span><span class="o">.</span><span class="n">private</span><span class="p">)</span> <span class="p">}</span> |
human.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">import</span> <span class="s">"fmt"</span> <span class="k">var</span> <span class="n">Public</span> <span class="kt">string</span> <span class="o">=</span> <span class="s">"Public"</span> <span class="k">var</span> <span class="n">private</span> <span class="kt">string</span> <span class="o">=</span> <span class="s">"private"</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="n">Age</span> <span class="kt">int</span> <span class="p">}</span> <span class="k">func</span> <span class="n">Say</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">"Human!"</span><span class="p">)</span> <span class="p">}</span> |
出力
private
は頭が小文字でunexpotedと出ている
1 2 |
.\main.go:19:14: cannot refer to unexported name mylib.private .\main.go:19:14: undefined: mylib.private |