Golang在GOROOT而不是GOPATH中查找包。

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

Golang looks for packages in GOROOT instead of GOPATH

问题

你的问题是在运行一个简单的Go程序时遇到了导入错误。根据你提供的信息,我看到你的文件夹结构如下:

C:\Users\User\Documents\Golang\src\MyProject\source
    main.go
    go.mod
    go.sum
    db/     几个.go文件
    webapp/ 几个.go文件

你已经运行了go mod init命令,并且你的GOPATH设置为C:\Users\User\Documents\Golang\,但它仍然在GOROOT中搜索本地导入模块。

根据你的描述,问题可能出在你的GOPATH设置上。请确保你的GOPATH设置正确,并且在运行程序之前,你已经在终端中使用go buildgo run命令来编译和运行你的程序。

另外,请确保你的导入路径是正确的。根据你的文件夹结构,你的导入语句应该是这样的:

import (
	"fmt"
	"MyProject/source/db"
	"MyProject/source/webapp"
)

请根据你的实际文件夹结构进行相应的调整。希望这些信息对你有帮助!

英文:

I'm trying to run a simple Go program, with the following setup:

// main.go

package main

import (
	"fmt"
	"db"
	"webapp"
)

func main() {
	database := db.New()
	webapp.StartServer(&database, &notifierClient)
}

I get the following import errors:

main.go:8:2: package db is not in GOROOT (C:\Program Files\Go\src\db)
main.go:9:2: package webapp is not in GOROOT (C:\Program Files\Go\src\webapp)

The folder structure I have is:

C:\Users\User\Documents\Golang\src\MyProject\source
    main.go
    go.mod
    go.sum
    db/     several .go files
    webapp/ several .go files

I've run go mod init and my GOPATH is set to C:\Users\User\Documents\Golang\ but it keeps searching for local import modules in GOROOT.

Where am I going wrong?

答案1

得分: 4

你需要在导入路径之前添加你的模块名称,例如myapp

import (
	"myapp/db"
	"myapp/webapp"
)

代码组织

导入路径是用于导入包的字符串。包的导入路径是其模块路径与模块内子目录的组合。例如,模块github.com/google/go-cmp包含一个位于cmp/目录中的包。该包的导入路径是github.com/google/go-cmp/cmp。标准库中的包没有模块路径前缀。


Go 1.16中的新模块更改

默认启用模块
现在,即使没有go.mod文件,go命令默认以模块感知模式构建包。这是在所有项目中使用模块的重要一步。

仍然可以通过将GO111MODULE环境变量设置为off来在GOPATH模式下构建包。您还可以将GO111MODULE设置为auto,仅在当前目录或任何父目录中存在go.mod文件时启用模块感知模式。这是以前的默认设置。请注意,您可以使用go env -w命令永久设置GO111MODULE和其他变量:

go env -w GO111MODULE=auto
我们计划在Go 1.17中停止对GOPATH模式的支持。换句话说,Go 1.17将忽略GO111MODULE。如果您有无法在模块感知模式下构建的项目,请现在进行迁移。如果有阻止您进行迁移的问题,请考虑提交一个问题或经验报告。


为了清晰起见 - 我的测试结构是:

go version
go version go1.17.5 linux/amd64

只是一个工作示例(main.go文件):

package main

import (
	"myapp/db"
	"myapp/webapp"
)

func main() {
	database := db.New()
	webapp.StartServer(&database, &notifierClient)
}

var notifierClient = "Test"

调试输出:

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

英文:

You need to add your module name e.g. myapp before the import path:

import (
	"myapp/db"
	"myapp/webapp"
)

Code organization:

> An import path is a string used to import a package. A package's import path is its module path joined with its subdirectory within the module. For example, the module github.com/google/go-cmp contains a package in the directory cmp/. That package's import path is github.com/google/go-cmp/cmp. Packages in the standard library do not have a module path prefix.


New module changes in Go 1.16:

Modules on by default
The go command now builds packages in module-aware mode by default, even when no go.mod is present. This is a big step toward using modules in all projects.

It’s still possible to build packages in GOPATH mode by setting the GO111MODULE environment variable to off. You can also set GO111MODULE to auto to enable module-aware mode only when a go.mod file is present in the current directory or any parent directory. This was previously the default. Note that you can set GO111MODULE and other variables permanently with go env -w:

go env -w GO111MODULE=auto
We plan to drop support for GOPATH mode in Go 1.17. In other words, Go 1.17 will ignore GO111MODULE. If you have projects that do not build in module-aware mode, now is the time to migrate. If there is a problem preventing you from migrating, please consider filing an issue or an experience report.


For the sake of clarity - My test structure is:

 go version
go version go1.17.5 linux/amd64

Golang在GOROOT而不是GOPATH中查找包。

Just a working example (main.go file):

package main

import (
	"myapp/db"
	"myapp/webapp"
)

func main() {
	database := db.New()
	webapp.StartServer(&database, &notifierClient)
}

var notifierClient = "Test"

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

Golang在GOROOT而不是GOPATH中查找包。

Debug Output:

Golang在GOROOT而不是GOPATH中查找包。

huangapple
  • 本文由 发表于 2021年12月11日 19:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70314880.html
匿名

发表评论

匿名网友

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

确定