ssk*_*ssk 6 shared go go-modules
我在我的项目中使用 go 模块。我在内部文件夹中共享了代码。
.
??? README.md
??? internal
? ??? shared
? ??? request.go
? ??? request_test.go
??? web
??? README.md
??? go
??? src
??? webservice
??? go.mod
??? go.sum
??? main.go
Run Code Online (Sandbox Code Playgroud)
使用 go 模块时,我无法从 webservice 访问内部/共享。我收到以下错误:
package internal/shared is not in GOROOT (/usr/local/go/src/internal/shared)
Run Code Online (Sandbox Code Playgroud)
在 main.go 中从 webservice 导入时:
import "internal/shared"
Run Code Online (Sandbox Code Playgroud)
注意:我正在尝试与上面未列出的另一个 mod 共享内部/共享。
如何解决这个问题?
您的go.mod内部web/go/src/webservice表明该包与您的包位于不同的模块中。\n当您将和移动到整个项目的根目录internal/shared时,它应该可以工作。然后和包将位于一个 内。go.modgo.sumweb/go/src/webserviceinternal/sharedgo module
这对我有用:
\n\n .\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.sum\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 internal\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 shared\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 request.go\n \xe2\x94\x82\xc2\xa0\xc2\xa0\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 web\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 go\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webservice\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\n\nRun Code Online (Sandbox Code Playgroud)\n\n并且在将包go-module导入到.internal/sharedmain.go
所以,在你的main.go导入里面应该看起来像import "$your-go-module/internal/shared"
有关内部包的更多信息请参见此处
\n我最终通过将 go.mod 添加到内部/共享并使用以下内容编辑 webservice 中的 go.mod 来修复:
module webservice
go 1.14
replace example.com/shared => ../../../../internal/shared/
require (
github.com/gorilla/mux v1.7.4
github.com/spf13/viper v1.6.3
github.com/stretchr/testify v1.5.1
example.com/shared v0.0.0-00010101000000-000000000000
)
Run Code Online (Sandbox Code Playgroud)
example.com/shared v0.0.0-00010101000000-000000000000 是由生成的"go mod init webservice"