LoginSignup
14
1

More than 3 years have passed since last update.

「Go 1.4 “Internal” Packages」の挙動を確認してみる

Posted at

はじめに

Go の internal パッケージの仕様について実際にコードを動かして確認します。

Go 1.4 “Internal” Packages
Internal packages | Go 1.4 Release Notes

要約

internal の一個上の階層のパスを見て、そのパスの下に配置されているパッケージからならアクセス可能です。たぶん。

架空の例

  • github.com/xxxx/foo/internal/bar というパッケージがある
  • internal の一個上は github.com/xxxx/foo
  • github.com/xxxx/foo の下に属する全てのパッケージは github.com/xxxx/foo/internal/bar にアクセス可能
  • github.com/xxxx/foo/buz/qux も github.com/xxxx/foo/internal/bar にアクセス可能

実際の例

実際にやってみた

前提

foo/**/main.go
package main

import (
    "foo/**/internal/**/hello"
)

func main() {
    hello.Hello()
}
foo/**/internal/**/hello/hello.go
package hello

import "fmt"

// Hello ...
func Hello() {
    fmt.Println("Hello, World!")
}

パターン1(呼べるやつ)

foo
├── internal
│   └── hello
│       └── hello.go
├── go.mod
└── main.go

foo/internalfoo から呼べる。

スクリーンショット 2019-11-21 14.09.24.png

パターン2(呼べないやつ)

foo
├── bar
│   └── internal
│       └── hello
│           └── hello.go
├── go.mod
└── main.go

foo/bar/internalfoo から呼べない。

スクリーンショット 2019-11-21 14.13.39.png

パターン3(呼べるやつ)

foo
├── bar
│   ├── internal
│   │   └── hello
│   │       └── hello.go
│   └── main.go
└── go.mod

foo/bar/internalfoo/bar から呼べる。

スクリーンショット 2019-11-21 14.16.03.png

パターン4(呼べるやつ)

foo
├── cmd
│   └── bar
│       └── main.go
├── internal
│   └── hello
│       └── hello.go
└── go.mod

foo/internalfoo/cmd/bar から呼べる。

スクリーンショット 2019-11-21 14.22.09.png

結論

internal の 一個上を基準に考えればよさそうです。

14
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
1