如何在VSCode上实现与GoLand一样完美的多模块支持(无需进行Go工作)

huangapple go评论100阅读模式
英文:

How to get GoLand's flawless multi module support on VSCode (no go work)

问题

当我在GoLand(Jetbrains)中打开我的多模块项目时,它表现得非常出色,实现类型声明和其他有用的IDE功能都能正常工作。

但是当我在VSCode中打开同一个项目时,无法让智能感知(使用gopls)工作,它不起作用,并且需要对模块结构进行严重更改,使用go work(所以这对我来说不是一个选择)

这是因为GoLand使用了不同的语言服务器吗?

我尝试了一些Go扩展设置的实验,但没有任何好处。我能够禁用语言服务器以避免所有内容变红,但这不是一个解决方案。

英文:

When I open my multi module project in GoLand (Jetbrains) it works amazingly, with Implementations, Type Declarations and other useful IDE features working fine out of the box.

But when I open that same project in VSCode, I cannot get intellisense to work (with gopls) it does not work and requires severe changes to the module structure with go work, (so it's not an option for me)

Is it because GoLand is using a different language server?

I tried experimenting with the Go extension settings which did not result in any benefit. I was able to disable the language server from turning everything red, but it is not a solution

答案1

得分: 1

go workspaces是工具包的一部分,应该可以在支持该功能的任何IDE上使用。我每天都在VS Code上使用它。

所以基本上你需要在项目根目录下有一个go.work文件,VS Code会解释它。例如:

go 1.20

use (
	.
	./mymodule1
	./mymodule2
)

不同之处在于,GoLand会自动添加多模块设置中的模块,而我不知道VS Code是否具备这个功能。

> 当你创建一个go.work文件时,GoLand会自动添加项目中的所有模块。

如果你查看Go的.gitignore文件,你会发现go.work文件默认被忽略,所以我认为你不需要将其纳入版本控制。我通常会注释掉那一行,这样VS Code就会跟踪它,并将其包含在我的提交中。

# Go workspace file
go.work

要将新模块添加到go.work中,只需从新模块的根目录执行go work use .命令。

有时我需要重新加载VS Code才能同步它,但通常它会立即生效。

如果你仍然遇到问题,那么可能值得尝试一下故障排除扩展。

英文:

go workspaces are part of the toolkit and should work on any IDE that supports the feature. I've been using it on VS Code on a daily basis.

So basically you need a go.work file in the project root and VS Code will interpret it. Example:

go 1.20

use (
	.
	./mymodule1
	./mymodule2
)

The difference is that GoLand automatically adds the modules in a multi-module setup, and I'm not aware of VS Code having this capability.

> When you create a go.work file, GoLand automatically adds all the modules in the project.

If you check Go's .gitignore you'll see that the go.work file is ignored by default, so I would assume that you don't have it versioned. I usually comment that line so that VS Code keeps track of it and it goes into my commits.

# Go workspace file
go.work

To add new modules to go.work simply do go work use . from a new module root.

Sometimes I have to reload VS Code to sync it, but usually it works right away.

If you're still having issues, then probably it is worth trying troubleshooting the extension.

huangapple
  • 本文由 发表于 2023年5月9日 03:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76203668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定