https://pkg.go.dev/cmd/go#hdr-Internal_packages
Code in or below a directory named “internal” is importable only by code that shares the same import path above the internal directory.
internalより上のimport pathが同じコードからしかimportできない。違反はコンパイル時にエラーになる- 言語仕様ではなく go コマンド(ビルドツール)のルール
- 用途: 「モジュール内では共有したいが、外部には公開APIとして約束したくない」コードを置く
myapp/ go.mod: module example.com/myapp
├── main.go OK(example.com/myapp 配下)
├── internal/db/db.go example.com/myapp 配下からのみimport可
└── api/internal/auth/ example.com/myapp/api 配下からのみimport可(main.go からは不可)
別モジュール(example.com/other)からimportすると:
package main
import "example.com/myapp/internal/db"
func main() { println(db.Hello()) }
main.go:3:8: use of internal package example.com/myapp/internal/db not allowed