Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang. Import only one file from package

Tags:

go

Hi all. I am new in GO and need some help. I've got a project with this structure

GO Project 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?

like image 995
trashgenerator Avatar asked Mar 31 '17 19:03

trashgenerator


Video Answer


2 Answers

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.

like image 197
Kaedys Avatar answered Oct 15 '22 03:10

Kaedys


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.

like image 33
lee8oi Avatar answered Oct 15 '22 04:10

lee8oi