lib/src 和 dart 中的 /bin 有什么区别?

What is the difference between lib/src and /bin in dart?

我知道 lib/ 是我们放置所有库文件的地方,而 /bin 是我们放置命令行应用程序入口点的地方。我知道它们都是 public lib/ 和 bin 但我无法理解使用 lib/src 的约定,根据官方文档应该包含:实现代码

lib/ 是包含可共享代码的目录。 可以分享

  • 到其他顶级目录,如 bin/web/example/test/tool/、...在同一个包中
  • 将此包作为依赖项的其他包。

lib/src 按照惯例包含由 lib/lib/xxx 公开的 public API 的私有实现,其中 xxx 不是 src.

bin 为命令行应用程序保留并包含执行它们的 Dart 入口点脚本(包含 main() {...} 的文件)。

pubspec.yaml 中,您可以定义可执行文件 https://www.dartlang.org/tools/pub/pubspec#executables,允许您从 bin/ 执行 运行 脚本,只需执行 foo 即可获得 dart somePath/bin/foo.dart 执行(使用 pub global activate my_package_with_foo)。

Pub Package Layout Conventions - Implementation files

The libraries inside lib are publicly visible: other packages are free to import them. But much of a package’s code is internal implementation libraries that should only be imported and used by the package itself. Those go inside a subdirectory of lib called src. You can create subdirectories in there if it helps you organize things.

You are free to import libraries that live in lib/src from within other Dart code in the same package (like other libraries in lib, scripts in bin, and tests) but you should never import from another package’s lib/src directory. Those files are not part of the package’s public API, and they might change in ways that could break your code.

When you use libraries from within your own package, even code in src, you can (and should) still use package: to import them.