Using go project locally

(Go newbie here)

If instead of using go get to install a library, I want to clone the repository locally to use it, what would I have to do?
My GOPATH is /home/reik/go

Just putting the cloned repository into my /home/reik/go and then going inside /home/reik/go/hep/ where hep is the name of the repository, and executing go build doesn’t work.

    [reik@reik-msi hep]$ go build
    can't load package: package hep: code in directory /home/reik/go/src/hep expects import "go-hep.org/x/hep"

Neither does go get

    [reik@reik-msi hep]$ go get
    go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src;
    	ignoring go.mod;
    	see 'go help modules'
    can't load package: package hep: code in directory /home/reik/go/src/hep expects import "go-hep.org/x/hep"

Looking at the go-hep.org/x/hep import, does this mean this library cannot be built locally?

1 Like

Hi, instead of cloning the repo to $GOPATH did you try

=> go get go-hep.org/x/hep.

This will download the package to /home/reik/go/src

Take a look at this line of the error can't load package: package hep: code in directory /home/reik/go/src/hep

So it is looking for hep inside of src directory of your $GOPATH.

1 Like

In the old school style you can put cloned repository in /your_project_name/vendor folder.

1 Like

But does this mean that I can modify the source code and rebuild it after getting it installed with go get?

1 Like

Yes, you can. But if you want to use a custom version of hep in your project vendoring is probably a good idea as @geosoft1 pointed out.

1 Like

So… does /your_project_name/vendor/ have to be in my GOPATH?

For example in this case,
the core package is in GOPATH/src/go-hep.org/x/hep, so would the package with my local changes be in GOPATH/src/go-hep.org/x/hep/vendor/hep?

Using go build in the root directory of the package? In this case GOPATH/src/go-hep.org/x/hep?

1 Like

Yes, you should have something like this GOPATH/src/your_project_name/vendor/other_package_name

1 Like

In your example, could your_project_name and other_package_name be the same? I mean… they are the same package. your_project_name being installed via go-get and other_package_name being the same package saved locally.

1 Like

Yes, could be the same although is better to keep things clear. Perhaps the name of the project should be different from the names of other packages. In my opinion is no reason to have two different things with the same name.

1 Like

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