-1

If I have a simple import such as:

import (
     "hello.io/example/client"
)

... should I assume that go mod tidy by definition, will try to “import” (or more precisely transitively resolve) all of

hello.io/example/

?

Details

In my case, I only need to import a few files from the example/client directory shown above. Is there a straight forward way to do this? Some ideas I had:

  • make a separate git repo for hello.io/example/client and hello.io/example/server. This obviously has drawbacks.
  • make a separate go.mod for example/client and example/server.
  • use some kind of go mod magic to tell go mod "only worry about importing things from the directory referenced in the import clause.

I would prefer the 3rd option, but I don't see anything in go mod documentation about wether or not importing a subfolder from an external project is allowed.

Summary of the problem statement

So to summarize, the more detailed version of this question is does go mod tidy have the ability to only import certain parts of a dependency, or is it an all or nothing operation, and if the latter, what is the best way to carve up dependencies so as not to "import the world" when simply depending on a few files in a monorepo ?.

I've also tagged this question as "kubernetes" because, specifically, it pertains to the more detailed example here: https://github.com/kubernetes-sigs/kpng/issues/58 .

jayunit100
  • 17,388
  • 22
  • 92
  • 167

1 Answers1

3

does go mod tidy have the ability to only import certain parts of a dependency [?]

No. It's either a dependency in which case the package is used or it's not a dependency. Note that files have no real meaning for Go and cannot be imported, used, tested, distributed, etc. (except as part of their package).

Volker
  • 40,468
  • 7
  • 81
  • 87