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

Time:09-27

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/[email protected]
  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
)

CodePudding user response:

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)

  •  Tags:  
  • go
  • Related