10

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?

2
  • 4
    Might be just me, but if you have millions of stuff in one package, it's time to refactor. Commented Mar 31, 2017 at 19:27
  • Man it's just an example. But if I had 10 - it's not so bad. But each of those ten has some big alogirithm for 500 strings Commented Mar 31, 2017 at 19:39

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks man. I don't know about "the compiler generally only includes in the compiled binary functions and types actually reference" you told.
Just to add to @kaedys , suppose you want to break your package/application to several files for readability purposes eg one file file-operations.go holding all structs , types related to file operations , server.go handling servers , main.go holding the main function , just put all file s in the package directory and name them your package name , go will compile all automatically .
I'm not sure about the compiler optimization given - it seems to include everything in each referenced package. I don't think it culls at any finer granularity than that. I haven't read the compiler/linker source, it's just the impression I've gotten from usage.
Just to add to this general packaging advice. Be sure you don't introduce circular dependencies. e.g. you can import "myapp/pack" from myapp and vice versa, but NOT BOTH ways at the same time.
Or any other way that would create a loop, such as a -> b -> c -> a.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.