How to import local package into another project?

I have set GOPATH=/home/oem/golib:/home/oem/code both include directories src, pkg and bin. My working place is ‘home/oem/code/’ where I simulated package in directory src/firstpkg and a project src/project where main.go is placed, both initiated with go mod init xyz. Despite reading through many posts I cannot resolve this problem on how to import local package firstpkg into the project, from which both are on GOPATH. Please see the print screen also including “go env”, it seems that golang is not looking into GOPATH despite being set. Any help very much appreciated!

Go modules do not look at the GOPATH at all. Please refer to the other module by its module name and use a replace directive to make it point to the local path.


Thank you so much. I was testing it and it works.

One sub question please, i think it fits at this spot as well. How to use absolute path in go.mod so it fits different users/computers, e.g. I want to reach package /home/myLaptop/SyncingFolder/package which conflicts if i use it on my Desktop machine /home/myDesktop/SyncingFolder/package. We cannot use import directive in go.mod in order to use os.Getenv("HOME") neither works home sign ~/. Relative paths like ../../../../somepkg can be used but is very unclear solution to what new package is pointing to. Any idea to that?

Example of would i would like: replace somepackage => ~/SyncingFolder/someotherpkg

replace with a local path is for dev only, don’t commit it that way and instead locally point it to the correct location.

1 Like

I’d just like to add that go workspaces with the new go 1.18 tackles the problem much simpler and better. These two references helped a lot:

The workspaces are also meant to replace the replace directives making things much simpler.

I always thought that GO111MODULE=auto does the job.

Have tried that one too, but did not work for me. Thanks.