Table of Contents
Question content
Solution
Home Backend Development Golang How to load dependencies locally using go workspace?

How to load dependencies locally using go workspace?

Feb 09, 2024 pm 04:15 PM
go language

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why does not report an error when DSN passes empty? When using sql.Open, why does not report an error when DSN passes empty? Apr 02, 2025 pm 12:54 PM

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...

See all articles