如何找到Go导入失败的问题?

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

How can I find the problem with Go failed import?

问题

在进行一个Go Web应用项目(用于学习)时,我遇到了以下问题:

一开始,一切都正常。我从标准库中导入了一些包,在代码中使用它们,一切都工作正常,直到我尝试导入用于PostgreSQL的pq驱动程序时。

我详细描述一下我所做的操作:
项目文件夹位于:notes。

项目所在目录为:C:\Users\david\go\src\github.com\davidkuch\notes

启动时,我运行了命令:go mod init。

我导入了标准包"database/sql"。

为了下载该包,我使用了命令:go get "github.com/lib/pq"

之后运行了go mod tidy

但是编译器报错:无法导入{package-name},没有所需的模块提供该包{package-name}

我尝试阅读相关主题的文档,但找不到我犯了什么错误的地方。我尝试安装另一个来自GitHub的包时也出现了同样的问题。

我应该去哪里查找问题呢?由于编译器说他"找不到",我花了很多时间检查命名和路径,但我确实在尝试导入的路径中看到了该包。更准确地说:

经过几个小时的自己尝试修复,我向您寻求一些帮助或解释发生了什么。

英文:

While working on a Go web-app project (for learning),
I have encountered the following issue:

At the beginning, everything was alright.
I imported packages from the standard library,
used them in the code and everything worked.
up to the moment when I have tried to import the pq driver for PostgreSQL.

The actions that I did in detail:
The folder with the project files inside: notes.

project is in the directory: C:\Users\david\go\src\github.com\davidkuch\notes

When starting, i run the command: go mod init.

I imported the standard package "database/sql".

to download the package I used: go get "github.com/lib/pq"

after that- go mod tidy

but the compiler says:could not import {package-name} no required module provides package
{package-name}

I tried to read through the docs of the related topics, but couldn't find where I did a mistake.
the same happens for another package i have tried to install from GitHub.

where should I be looking to find the problem?
as the compiler says that he "cannot find", I made a lot of effort checking naming and paths.
but I see the package exactly in the path I try to import from.
to be more precise:

After some hours of trying to fix that by myself, I ask You for some help or explanation of what is happening.

答案1

得分: 2

你不需要这样做。只需创建一个文件夹,比如:C:\Users\david\notes

然后创建C:\Users\david\notes\main.go文件:

package main

import (
   "database/sql"
   _ "github.com/lib/pq"
)

func main() {
   println(sql.ErrNoRows) 
}

然后进行构建:

go mod init something
go mod tidy
go build
英文:

> project is in the directory: C:\Users\david\go\src\github.com\davidkuch\notes

You don't need to do that. Just make a folder like: C:\Users\david\notes.

Then make C:\Users\david\notes\main.go:

package main

import (
   "database/sql"
   _ "github.com/lib/pq"
)

func main() {
   println(sql.ErrNoRows) 
}

Then build:

go mod init something
go mod tidy
go build

huangapple
  • 本文由 发表于 2021年6月8日 23:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/67889997.html
匿名

发表评论

匿名网友

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

确定