How to use a folder with several custom packages for all my projects?

I have many projects that do different things each, and most of them use packages that I have already created before.

For example in the custom directory I have these three packages:
C:\custom\package_name_a
C:\custom\package_name_b
C:\custom\package_name_c

I want to use custom in all my projects, like this:

import(
	"custom/package_name_a"
	"custom/package_name_b"
	"custom/package_name_c"
)

I solved it by copying the custom folder to the directory:
C:\Program Files\Go\src

That way I can import as in the example I showed before, my problem comes when using the go mod vendor command it copies all the dependencies of my project to a folder called vendor but totally ignores my custom dependencies.

So what can I do to make Go detect my dependencies as vendor repositories?

Any ideas would be welcome but I have to clarify that custom cannot be uploaded to the internet.

The key is to use replace in your go mod file to point to your local modules. You’ll need a go.mod in your local packages to make them modules. Here’s a description https://levelup.gitconnected.com/import-and-use-local-packages-in-your-go-application-885c35e5624

1 Like

It is not comfortable to use.

Then you need to push your modules to GitHub or any other compatible service.

What do you call that kind of service where Go requests the package from a server? To see if I can create a local version.

Or can you help me with a link to a tutorial on this? Because the results I find don’t help.

I’m pretty sure that’s because you put your custom folder into C:\Program Files\Go\src, so the compiler thinks custom is a package in the standard library and shouldn’t go into vendor.

The only way that I know how to use a different package that isn’t online is using the replace directive in the go mod file like mje said. Basically:

my-go-src-directory/
    custom/
    ...
    my-other-project/
        go.mod

in my-go-src-directory/my-other-project/go.mod (and every other project that uses custom), I have:

module my-other-project

go 1.18

require (
	github.com/skillian/custom v0.0.0-00010101000000-000000000000
)

replace (
	http://github.com/skillian/custom => ../custom
)

This works even if custom never gets uploaded to github.com.

2 Likes

goproxy

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.