英文:
Why go get in a repository with go.work throws warning package is not used in this module?
问题
我的目标是创建一个具有多个文件夹的存储库。
|- go.work
|- websocket
| |- go.mod
| |- go.sum
| |- server.go
|- channel
| |- main.go
websocket文件夹使用github.com/gorilla/websocket
包。
所以,我需要在websocket
文件夹中执行以下操作。
$ go mod init github.com/kidfrom/learn-golang/websocket
$ go get github.com/gorilla/websocket@v1.5.0
$ go work use .
问题是,websocket/go.mod
会抛出警告
github.com/gorilla/websocket is not used in this module
如果我执行go mod tidy
,websocket/go.mod
将被清除,websocket/server.go
会抛出错误
could not import github.com/gorilla/websocket (no required module provides package "github.com/gorilla/websocket")
TLDR
websocket/go.mod
go 1.19
require github.com/gorilla/websocket v1.5.0 // indirect
websocket/go.sum
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
go.work
use (
./websocket
)
英文:
My goal is to create a repository with multiple folders.
|- go.work
|- websocket
| |- go.mod
| |- go.sum
| |- server.go
|- channel
| |- main.go
The websocket uses github.com/gorilla/websocket
package.
So, I need to do in the websocket
folder.
$ go mod init github.com/kidfrom/learn-golang/websocket
$ go get github.com/gorilla/websocket@v1.5.0
$ go work use .
The problem is, the websocket/go.mod
throws warning
github.com/gorilla/websocket is not used in this module
If I do go mod tidy
, the websocket/go.mod
will be cleaned out and websocket/server.go
will throws error
could not import github.com/gorilla/websocket (no required module provides package "github.com/gorilla/websocket")
TLDR
websocket/go.mod
module github.com/kidfrom/learn-golang/websocket
go 1.19
require github.com/gorilla/websocket v1.5.0 // indirect
websocket/go.sum
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
go.work
go 1.19
use (
./websocket
)
答案1
得分: 0
首先,请确保你的 server.go
文件位于 package main
中,就像原始的 gorilla/websocket/examples/echo/server.go
文件一样。
其次,测试一下文件夹的名称(也是 websocket
)是否会引发问题(与 websocket.xxx
的调用冲突)。为了测试,请尝试更改文件夹名称(并相应地更新 go.work
)。
英文:
First, make sure your server.go
is in package main
, as the original gorilla/websocket/examples/echo/server.go
is.
Second, test if the name of the folder (which is also websocket
) is an issue (conflicting with the websocket.xxx
calls).
For testing, try and change it (and update go.work
accordingly)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论