Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing local changes of a package without pushing code in Golang

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

like image 310
Imran Latif Avatar asked Aug 29 '16 21:08

Imran Latif


People also ask

Which one is the correct way to import multiple packages libraries in Golang?

Both single and multiple packages can be imported one by one using the import keyword.

Can we import main package in Golang?

You cannot import the main package. Any shared code should go in a separate package, which can be imported by main (and other packages). See also groups.google.com/forum/#! topic/Golang-nuts/frh9zQPEjUk for discussion.

What is GO111MODULE?

GO111MODULE is an environment variable that can be set when using go for changing how Go imports packages. One of the first pain-points is that depending on the Go version, its semantics change.


1 Answers

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
like image 158
Marcello de Sales Avatar answered Oct 09 '22 20:10

Marcello de Sales