[英]What is the difference between lib/src and /bin in dart?
I know that lib/ is where we put all our library files and /bin is where we put our entrypoint for our command-line app. 我知道lib /是存放所有库文件的位置,/ bin是存放命令行应用程序入口点的位置。 I know both of them are public lib/ and bin but i'm unable to understand the convention of using lib/src which according to the official docs should contain: implementation code
我知道它们都是公共lib /和bin,但我无法理解使用lib / src的约定,根据官方文档,该约定应包含: 实现代码
lib/
is the directory that contains shareable code. lib/
是包含可共享代码的目录。 It can be shared 可以共享
bin/
, web/
, example/
, test/
, tool/
, ... in the same package bin/
, web/
, example/
, test/
, tool/
,... lib/src
by convention contains the private implementation of the public API exposed by lib/
or lib/xxx
where xxx
is not src
. 按照惯例,
lib/src
包含由lib/
或lib/xxx
公开的公共API的私有实现,其中xxx
不是src
。
bin
is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain main() {...}
). bin
是为命令行应用程序保留的,包含执行它们的Dart入口点脚本(包含main() {...}
)。
In pubspec.yaml
you can define executables https://www.dartlang.org/tools/pub/pubspec#executables that allows you to run scripts from bin/
by just executing foo
to have dart somePath/bin/foo.dart
executed (using pub global activate my_package_with_foo
). 在
pubspec.yaml
您可以定义可执行文件https://www.dartlang.org/tools/pub/pubspec#executables ,该文件使您可以通过执行foo
来执行dart somePath/bin/foo.dart
,从而从bin/
运行脚本(使用pub global activate my_package_with_foo
)。
See Pub Package Layout Conventions - Implementation files 请参见发布包布局约定-实施文件
The libraries inside
lib
are publicly visible: other packages are free to import them.lib
内部的lib
是公开可见的:其他软件包可以自由导入。 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
calledsrc
.它们位于
lib
的子目录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 inlib
, scripts inbin
, and tests) but you should never import from another package'slib/src
directory.您可以从同一个程序包中的其他Dart代码中导入位于
lib/src
中的库(就像lib
其他库,bin
和测试中的脚本一样),但是绝对不要从另一个包的lib/src
目录中导入。 Those files are not part of the package's public API, and they might change in ways that could break your code.这些文件不是软件包公共API的一部分,它们可能会以破坏代码的方式进行更改。
When you use libraries from within your own package, even code in
src
, you can (and should) still usepackage:
to import them.当您从自己的程序包中使用库时,即使是
src
代码,也可以(并且应该)仍然使用package:
来导入它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.