Home > Backend Development > Golang > How to load dependencies locally using go workspace?

How to load dependencies locally using go workspace?

王林
Release: 2024-02-09 16:15:09
forward
644 people have browsed it

How to load dependencies locally using go workspace?

php editor Baicao introduces you how to use go workspace to load dependencies locally. When developing Go language projects, we often use various third-party libraries and dependencies. In order to facilitate the management and loading of these dependencies, the Go language provides a powerful workspace mechanism. By setting the workspace path and using the go mod command, we can quickly and easily load and manage the dependencies required for the project, improving development efficiency and code quality. Let's take a closer look at how to use go workspace to load dependencies locally.

Question content

I have a go workspace project that contains some go modules. One module depends on another module in the same workspace. I want the dependencies to be resolved locally without downloading from the internet.

I have two modules, hello and example. hello depends on example. Its structure is:

example
  - go.mod
  - hello
     - go.mod
hello
  - go.mod
go.work
Copy after login

go.work is:

go 1.19

use (
    ./example/hello
    ./hello
)
Copy after login

Module hello requires the use of functions declared from the example -> hello module. go.mod under example/hello is:

module github.com/example/hello

go 1.19
Copy after login

When I run go get github.com/example/hello in the hello directory, an error occurs:

go: module github.com/example/hello: git ls-remote -q origin in /Users/joey/go/pkg/mod/cache/vcs/85c69767672480b072ae4eaec76fbf39ef710d025b14e923dd131ac218c9034c: exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
Copy after login

It seems go still wants to download dependencies from github.com instead of looking at the local worksapce. Is there a way to force go to look in the local directory first?

The complete code is: https://github.com/zhaoyi0113/go-workspace

Solution

The module name in the go.mod file is related to the project structure. Otherwise, random naming may cause conflicts with other libraries, making it unclear which library to pull when building dependencies.

In other words, the directory structure must match to get code from GitHub. Therefore, change the module name in the go.mod file under the "example" directory to:

module github.com/zhaoyi0113/go-workspace/example
Copy after login

For the "hello" directory:

module github.com/zhaoyi0113/go-workspace/hello
Copy after login

If the "hello" directory needs to depend on the "example" module, import it as follows:

require (
    github.com/zhaoyi0113/go-workspace/example xxxx
)
Copy after login

Here, if you created a version, "xxxx" can be the version number. If not, you can use the Git commit hash from the code you pushed to GitHub. You can find the Git hash for "example" on the GitHub project page. In the "hello" directory, execute the following command:

go get github.com/zhaoyi0113/go-workspace/example@{git version prefix, which can be found on the GitHub project page}
Copy after login

However, this method only allows you to get code that has been pushed to GitHub. If you want to quickly debug development locally, you can add the "replace" directive to the go.mod file:

replace github.com/zhaoyi0113/go-workspace/example => {local directory of your example}
Copy after login
<小时/>

renew

It is recommended to use the package approach to organize the project structure, where modules should be more independent and usually located in different repositories.

.
├── example
│    ├── reverse
│    └── utils.go
├── hello
│    └── hello.go
├── go.mod
├── go.sum
└── main.go
Copy after login

The above is the detailed content of How to load dependencies locally using go workspace?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template