nyxsonsleep
V2EX  ›  Go 编程语言

Go 可以运行, vscode 语法检查提示找不到包

By nyxsonsleep at 2022 年 8 月 30 日 · 1680 次点击

代码块

package main
import(
	"github.com/gocolly/colly/v2"
	"fmt"
)
func main() {
	// create a new collector
	c := colly.NewCollector()
	fmt.Println(c)
}

运行结果

Requests made: 0 (0 responses) | Callbacks: OnRequest: 0, OnHTML: 0, OnResponse: 0, OnError: 0

这行"github.com/gocolly/colly/v2"飘红,提示:

error while importing github.com/gocolly/colly/v2: cannot find package "github.com/saintfish/chardet" in any of

能用,但是该怎么解决飘红的问题。有两个 gopath ,目前在第二个 gopath 目录下的 src 目录的下级目录中运行,有 go.mod,go.sum 。

2 条回复
Vegetable
   1
Vegetable  
   2022 年 8 月 30 日
如果不是什么历史遗留原因的话,还是把项目从 gopath 里边移出来吧,go 都出到 1.19 了,大家可能已经告别 gopath 很久了。
你这个症状看起来,就是 gopls 并没有找到这个包,但是 go 命令能识别到。看起来是 gopls 去 gopath 里边找了,但是 go 命令能正确识别 go mod 安装的包。

极大概率可以通过移出来解决问题
nyxsonsleep
   2
nyxsonsleep  
OP
   2022 年 8 月 30 日
@Vegetable 非常感谢,看来是有了 mod 之后不再需要 gopath 了,删除环境变量后正常了。
The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
© 2026 V2EX · 23ms · 3.9.8.5