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. 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
src files are your raw, human-readable source code (in this case, .java ). bin files are your compiled code (in this case, .class )
"bin" is used for executables, "share" for data files, "lib" for shared libraries and so on. So if your program is a library, you can install it by default to /usr/local/lib. If it's a normal program, you can have it install to /usr/local/bin with your data files in /usr/local/share.
A library in a programming language represents a collection of routines (set of programming instructions). Dart has a set of built-in libraries that are useful to store routines that are frequently used. A Dart library comprises of a set of classes, constants, functions, typedefs, properties, and exceptions.
Dart provides the part keyword to allow you to split code into separate files in a library. It's used in the same file as the library keyword and needs to provide a relative path to the other source files that make up the library: for example, part "functions. dart"; . You can create new, empty text files for classes.
lib/
is the directory that contains shareable code.
It can be shared
bin/
, web/
, example/
, test/
, tool/
, ... in the same packagelib/src
by convention contains the private implementation of the public API exposed by lib/
or lib/xxx
where xxx
is not src
.
bin
is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain 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
).
See 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 oflib
calledsrc
. 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. 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 usepackage:
to import them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With