10

I' am learning Golang now-a-days and a total newbie. I have a question regarding packages.

Consider the following scenario:

Imagine I have a package github.com/ilatif/A in which I' am importing another package github.com/ilatif/B, like:

import "github.com/ilatif/B"

Now since both github.com/ilatif/A and github.com/ilatif/B are my packages and I' am working locally on them, is there a way to pull changes from github.com/ilatif/B package without pushing code to GitHub? As per Golang's documentation, I need to push the code to its relevant repo but I was wondering if there is such a way to pull local changes of my own package without pushing it to upstream first.

Thanks

JimB
  • 104,193
  • 13
  • 262
  • 255
Imran Latif
  • 985
  • 8
  • 21
  • 5
    You don't need to push or pull anything, they are just files in your GOPATH. – JimB Aug 29 '16 at 21:43
  • Then why does my local changes are not being updated? There might be some problems with my $GOPATH? I' am using gvm. – Imran Latif Aug 29 '16 at 21:46
  • 4
    If they're not being updated, then you're not editing the version in your GOPATH. It's probably easier to use the go tools directly (without gvm) to understand how they work first. Start here if you haven't yet: [How to Write Go Code](https://golang.org/doc/code.html) – JimB Aug 29 '16 at 21:50
  • Thanks for commenting. BTW, packages installed in the vendor folder is always getting precedence over my changes in packages. I' am confused. – Imran Latif Aug 29 '16 at 21:54
  • Yes, `vendor/` takes precedence of GOPATH. That's why I suggested starting with the basics using only the go tools, and adding other functionality when you find you need it. If you need to use a vendor directory, you need to update the code in there, usually using 3rd party tools to do so. – JimB Aug 29 '16 at 21:59
  • Thanks for explaining the `vendor/` thing. Basically I' am working on an already developed App which was using `vendor` folder. So just to clear, if the former developer might not have been using `vendor` then changing code in one of my local packages would have been reflected automatically? But since the former developer has been using `vendor` folder thing I need to use a tool which would update code in `vendor` folder whenever I update a package? Am I right? – Imran Latif Aug 29 '16 at 22:05
  • Yes. https://golang.org/cmd/go/#hdr-Vendor_Directories. You can use the vendor directory without any tooling, it's just cumbersome to update the files and keep them in sync. – JimB Aug 29 '16 at 22:09

1 Answers1

15

Go Module Solution

I could successfully use Golang with modules by using replace in the file go.mod.

https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

  • As soon as you save the go.mod file with the replace statement, Goland recognizes the updated module.

Example

  • Using replace MODULE_URL => /PATH/TO/MODULE/DIR
    • No need to specify the version
module github.x.com/services-x/x

go 1.13

require (
    github.com/briandowns/spinner v1.8.0
    github.com/golang/protobuf v1.3.1
    github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
    github.com/marcellodesales/cloner v0.0.0-20191126082454-c952bef1e067
    github.com/mitchellh/go-homedir v1.1.0
    github.com/mitchellh/mapstructure v1.1.2
    github.com/sirupsen/logrus v1.2.0
    github.com/spf13/cobra v0.0.5
    github.com/spf13/viper v1.4.0
    github.com/thoas/go-funk v0.4.0
    gopkg.in/src-d/go-git.v4 v4.13.1
    gopkg.in/yaml.v2 v2.2.2
)

replace github.com/marcellodesales/cloner => /Users/mdesales/dev/github.com/marcellodesales/cloner
rd3n
  • 4,440
  • 1
  • 33
  • 45
Marcello DeSales
  • 21,361
  • 14
  • 77
  • 80
  • Yeah I'm using same solution but having issues to make vs code recognize those changes. I believe the go language server (gopls) is saving some king of cache somewhere. – HNL May 29 '21 at 23:51