Why go get in a repository with go.work throws warning package is not used in this module?

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

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文件夹中执行以下操作。

  1. $ go mod init github.com/kidfrom/learn-golang/websocket
  2. $ go get github.com/gorilla/websocket@v1.5.0
  3. $ go work use .

问题是,websocket/go.mod会抛出警告

github.com/gorilla/websocket is not used in this module

如果我执行go mod tidywebsocket/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=

websocket/server.go

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.

  1. $ go mod init github.com/kidfrom/learn-golang/websocket
  2. $ go get github.com/gorilla/websocket@v1.5.0
  3. $ 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=

websocket/server.go

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)

huangapple
  • 本文由 发表于 2022年9月27日 12:11:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/73862295.html
匿名

发表评论

匿名网友

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

确定