How to support multiple go mod modules in a single repository

藏色散人
Release: 2021-07-13 14:33:44
forward
3397 people have browsed it

Background

I am writing Go recently. There is a project with multiple modules. The versions are released together. In order to use these modules for other projects, it is necessary Implement the release of multiple modules in one warehouse.

Warehouse structure

The warehouse structure is as follows:

.├── README.md
├── a
│   ├── a.go
│   └── go.mod
└── b
    ├── b.go
    └── go.mod
Copy after login

a/go.mod is generated using the following command:

go mod init github.com/robberphex/go-test-multi-module/a
Copy after login

Version release

Because it is multi-module, different tags are used. The tag names are uniformly <module name>/<version number>.

For example, the current tags are:

$ git tag
a/v1.0.0
b/v1.0.0
b/v1.0.1
Copy after login

User usage

When installing, users only need to execute the same command as a normal module:

$ go get github.com/robberphex/go-test-multi-module/b
go: downloading github.com/robberphex/go-test-multi-module/b v1.0.1
go: downloading github.com/robberphex/go-test-multi-module v0.0.0-20210420014022-d2c7262e89eb
$ go get github.com/robberphex/go-test-multi-module/a
go: downloading github.com/robberphex/go-test-multi-module/a v1.0.0
Copy after login

VSCode development experience

However, in the case of multiple modules, if you use VSCode to open the warehouse (root directory), VSCode will report an error, involving the automatic complement of third-party packages. None of them work:


I checked the information according to the prompts and found that golps only supports multi-module warehouses experimentally and needs to be manually Turn on this switch:

"gopls": {
    "build.experimentalWorkspaceModule": true}
Copy after login

This way you can develop multi-module projects smoothly.

P.S. There is also a switch ui.completion.experimentalPostfixCompletions that is also very useful. You can turn it on and see. The effect is as follows:

For more golang related technical articles, please visit the golang tutorial column!

The above is the detailed content of How to support multiple go mod modules in a single repository. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.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