Home  >  Article  >  Backend Development  >  Golang, problem of moving from local import path to remote import path

Golang, problem of moving from local import path to remote import path

王林
王林forward
2024-02-09 08:27:38596browse

Golang, problem of moving from local import path to remote import path

php editor Baicao will answer your question about moving from a local import path to a remote import path in Golang. In Golang development, we often encounter the situation of moving from the local import path to the remote import path, especially when multiple people collaborate on development or project migration. This article will detail how to handle this issue correctly to ensure your code runs smoothly. Let’s take a look!

Question content

I have just started breaking my application into different repositories. Like many people, I'm going through the trouble of dealing with Go and changing repositories. But there are many questions and answers to these questions, so I won't ask them here.

Instead, I have a simple question but I can't understand it. All my modules, since it's a big application, are just local references. For example core/validate etc.

Move content into multiple repositories. Seemed to cause problems with local references, so I changed the path to the remote path, e.g. gitlab.com/<group>/core.git/validate</group> as several Q&A suggested.

However, this will cause a problem, if gitlab.com/<group>/core.git/config</group> refers to gitlab.com/<group>/core. git/validate</group> does not mean that gitlab.com/ <group>/core.git/config</group> now points to pck/mod/gitlab.com/... Are some of the content either pointing to the remote server, or anywhere but each other, like when they only have relative paths like core/validate? When I change them it looks like this because my IDE (GoLand) shows the reference not found.

I haven't tried doing go mod init and rebuilding the mod from scratch, but go mod tidy doesn't work and I don't know if I have to go as well. It's fine to change the remote path in work, but now I just can't find the import shown in the IDE.

It seems that if you use a remote reference to make changes you make in your code while developing, you have to push the changes so that it is where the reference points and maybe do go get Change to reference local remote path, this seems like a bad way of developing and therefore can't be correct.

So, how do these remote paths work with development, what am I missing?

Solution

There are several concepts that can cause confusion.

A module is a collection of packages. You can name your module "mymodule" and then all packages under "mymodule" will be named "mymodule/pkg1", "mymodule/pkg2/otherpkg", etc.

Then you have the import path of the package. The import path shows the location of the package. For example, if your source code is in "mymodule/pkg1" and you import "mymodule/pkg2", then this is a reference to the package under the same module.

Now assume you have another module named "othermodule" on "github.com/mygroup/othermodule". You import a package in this module as "github.com/mygroup/othermodule/pkg1". If "othermodule/pkg1" references "othermodule/pkg2", then it still imports "othermodule/pkg2" because it is in the same module. But from "mymodule/pkg1" you import it as "github.com/mygroup/othermodule/pkg2".

The Go module system uses version references from other modules. When you include a package from a module, the specific version of the module is added to go.mod. If you push new changes to the module, you must update the reference to include those changes. That's why it's best not to split a tightly coupled project into multiple modules.

If you want to develop multiple modules together, use the "replace" directive to use a local copy of the module instead of pointing to the version on the repository.

The above is the detailed content of Golang, problem of moving from local import path to remote import path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete