Goland无法识别模块。

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

Goland doesnt recognize module

问题

我在使用Goland时遇到了无法识别导入的问题。

所以,像这样的基本示例结构

 - main.go
 - go.mod
 - folder/something.go

来自folder的导入无法被识别。

例如,在main.go中的下面导入是无效的,Goland会显示Cannot resolve directory somename的错误信息:

import "somename/folder"

go.mod是使用go mod init somename进行初始化的。

为什么会发生这种情况?为什么Goland无法将somename解析为我创建的模块?

英文:

I'm having issues with Goland recognizing my imports.

So, with a basic sample structure like this

 - main.go
 - go.mod
 - folder/something.go

imports from folder arent recognized.

E.g. below import, within main.go isn't valid, and Goland complains with the message Cannot resolve directory somename

import "somename/folder"

The go.mod was initialized with go mod init somename

Why is this happening? Why Goland can't resolve somename to the module that I created?

答案1

得分: 11

参考:JetBrains GoLand文档:使用Go模块

在GoLand的文档中提到,如果你没有从IDE本身创建Go项目,那么你需要手动配置它来使用Go模块。

如果你在IDE中创建了一个新的Go模块项目,Go模块已经被启用。如果你从Github上拉取了你的Go模块项目,你需要手动启用Go模块。

所以,我猜这就是你面临的问题所在。但是你可以按照文档的说明正确配置你的IDE。

英文:

Refer: JetBrains GoLand Docs: Working with Go modules

In GoLand's docs, it is mentioned that if you haven't created the Go project from the IDE itself then you have to configure it to use Go modules manually.

> If you create a new Go modules project in the IDE, Go modules are already enabled. If you pulled your Go modules project from Github, you need to enable Go modules manually.

So, I assume that's why you are facing the problem. But you can follow the docs and configure your IDE correctly.

答案2

得分: 2

如果IDE无法识别您的内部包,则:

  1. 在设置中找到Go Modules
  2. 启用启用Go模块集成复选框。
英文:

If the IDE is unable to recognize your internal packages then:

  1. Inside Settings find Go Modules
  2. Enable the Enable Go modules integration checkbox.

huangapple
  • 本文由 发表于 2021年12月21日 20:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70435420.html
匿名

发表评论

匿名网友

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

确定