Hi all. I am new in GO and need some help. I've got a project with this structure
But let's pretend that I have 100500 items in my logic package. What if I need import only one or two files for the specific package. Can I do it or I only can import a full package?
No, in Go you import packages, not files. However, the compiler generally only includes in the compiled binary functions and types actually referenced, so even if you include a massive package in your import (which is discouraged anyway), they'll not usually be included in the final binary unless needed.
And as RayfenWindspear pointed out, if a package is large enough that you want to only import a file or two, that's probably a pretty good smell test that you need to refactor that package.
I realize this question is largely already answered. But here are my thoughts anyways.
If the specific files can work independently from the rest of the package or do so with some minor modifications you can just simply copy those files to the project you want to use them in. Then reference the functions/methods/etc directly. But if you are using parts of a big package in various projects it would definitely be more suitable to break it into smaller packages. Then you can use those parts independently in as many projects as you wish without the excess baggage.
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