packages – When you build a binary in languages like C, Swift, or Go, and import dependencies, does it import the entire dependency?


By this I mean, say I import a module that looks like this:

function foo() {
  
}

function bar() {
  
}

I then in my code I import this module, but only use foo. In the module, there is no dependency of foo on bar. Does it include bar in the final build, or does it somehow know how to optimize it out? If it does optimize it out, how can it tell it is not required? If C, Go, and Swift don’t do this, does any language do this?

I would like to implement a module system where, when you request a module, it only fetches the specific things that you need. But in thinking about this, it seems like a hard problem. But this way, you could have a module with gigabytes of stuff, but only actually import a few KB or something.

The solution to this is to usually manually create smaller packages, like lodash in JavaScript world did by creating all their tiny individual-function packages. But I would like to do this sort of thing automatically.