英文:
Golang: no required module provides package
问题
我创建了一个包含main.go文件的新文件夹,并且包含以下代码:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
我运行了go mod init gin
和go mod tidy
。
go.mod和go.sum文件已创建,并且看起来正常。
我运行了go run main.go
并得到以下输出:
main.go:6:2: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
运行go get github.com/gin-gonic/gin
没有解决问题。
似乎main.go没有使用当前目录下的go.mod文件。我尝试设置GO111MODULE="auto"和GO111MODULE="on"都没有成功。
我的go env如下:
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/xxx/files/Programming/go-playground/gin/go.mod"
GOWORK="/home/xxx/files/Programming/go-playground/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3669122409=/tmp/go-build -gno-record-gcc-switches"
解决方案:
我的代码示例位于go工作区中,我忘记使用go work use
将其添加到工作区中。
英文:
I created a new folder with main.go and with following code:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
I run go mod init gin
and go mod tidy
.
go.mod and go.sum are created and they are looking fine.
I run go run main.go
and get this output
main.go:6:2: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin
go get github.com/gin-gonic/gin
doesn't solve the issue
It seems like main.go doesn't use go.mod from current directory. I tried setting GO111MODULE="auto" and GO111MODULE="on" without success.
my go env:
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/xxx/files/Programming/go-playground/gin/go.mod"
GOWORK="/home/xxx/files/Programming/go-playground/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3669122409=/tmp/go-build -gno-record-gcc-switches"
SOLVED:
my code example was inside a go workspace and I forgot to add it to the workspace with go work use
答案1
得分: 5
我可以按照以下步骤启动HTTP服务:
目录结构
./Playground/
├── go.mod
├── go.sum
└── main.go
步骤1:初始化模块
go mod init gin
go mod tidy
步骤2:添加gin依赖
go get github.com/gin-gonic/gin
步骤3:创建main.go文件,并将问题中提供的代码复制进去
步骤4:运行
go run ./main.go
英文:
I am able to start http service with following steps
Directory structure
./Playground/
├── go.mod
├── go.sum
└── main.go
Step.1 module init
go mod init gin
go mod tidy
Step.2 Add gin dependency
go get github.com/gin-gonic/gin
Step.3 Create main.go, with code given in question
Step.4 Run
go run ./main.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论