Home  >  Article  >  Backend Development  >  How to support multiple go mod modules in a single repository

How to support multiple go mod modules in a single repository

藏色散人
藏色散人forward
2021-07-13 14:33:443396browse

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

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

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

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

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

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}

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!

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