Using go project locally

(Pratyush Das) #1

(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 ""

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 ""

Looking at the import, does this mean this library cannot be built locally?

(Ashish Shekar) #2

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

=> go get

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.

(George Calianu) #3

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

(Pratyush Das) #4

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

(Ashish Shekar) #5

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.

(Pratyush Das) #6

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

For example in this case,
the core package is in GOPATH/src/, so would the package with my local changes be in GOPATH/src/

Using go build in the root directory of the package? In this case GOPATH/src/

(George Calianu) #7

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

(Pratyush Das) #8

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.

(George Calianu) #9

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.