Golang:没有必需的模块提供包。

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

Golang: no required module provides package

问题

我创建了一个包含main.go文件的新文件夹,并且包含以下代码:

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. r := gin.Default()
  8. r.GET("/ping", func(c *gin.Context) {
  9. c.JSON(http.StatusOK, gin.H{
  10. "message": "pong",
  11. })
  12. })
  13. r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
  14. }

我运行了go mod init gingo mod tidy

go.mod和go.sum文件已创建,并且看起来正常。

我运行了go run main.go并得到以下输出:

  1. main.go:6:2: no required module provides package github.com/gin-gonic/gin; to add it:
  2. go get github.com/gin-gonic/gin

运行go get github.com/gin-gonic/gin没有解决问题。

似乎main.go没有使用当前目录下的go.mod文件。我尝试设置GO111MODULE="auto"和GO111MODULE="on"都没有成功。

我的go env如下:

  1. GO111MODULE="auto"
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOCACHE="/home/xxx/.cache/go-build"
  5. GOENV="/home/xxx/.config/go/env"
  6. GOEXE=""
  7. GOEXPERIMENT=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/home/xxx/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/home/xxx/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org,direct"
  19. GOROOT="/usr/local/go"
  20. GOSUMDB="sum.golang.org"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.18.3"
  25. GCCGO="gccgo"
  26. GOAMD64="v1"
  27. AR="ar"
  28. CC="gcc"
  29. CXX="g++"
  30. CGO_ENABLED="1"
  31. GOMOD="/home/xxx/files/Programming/go-playground/gin/go.mod"
  32. GOWORK="/home/xxx/files/Programming/go-playground/go.work"
  33. CGO_CFLAGS="-g -O2"
  34. CGO_CPPFLAGS=""
  35. CGO_CXXFLAGS="-g -O2"
  36. CGO_FFLAGS="-g -O2"
  37. CGO_LDFLAGS="-g -O2"
  38. PKG_CONFIG="pkg-config"
  39. 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:

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. r := gin.Default()
  8. r.GET("/ping", func(c *gin.Context) {
  9. c.JSON(http.StatusOK, gin.H{
  10. "message": "pong",
  11. })
  12. })
  13. r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
  14. }

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

  1. main.go:6:2: no required module provides package github.com/gin-gonic/gin; to add it:
  2. 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:

  1. GO111MODULE="auto"
  2. GOARCH="amd64"
  3. GOBIN=""
  4. GOCACHE="/home/xxx/.cache/go-build"
  5. GOENV="/home/xxx/.config/go/env"
  6. GOEXE=""
  7. GOEXPERIMENT=""
  8. GOFLAGS=""
  9. GOHOSTARCH="amd64"
  10. GOHOSTOS="linux"
  11. GOINSECURE=""
  12. GOMODCACHE="/home/xxx/go/pkg/mod"
  13. GONOPROXY=""
  14. GONOSUMDB=""
  15. GOOS="linux"
  16. GOPATH="/home/xxx/go"
  17. GOPRIVATE=""
  18. GOPROXY="https://proxy.golang.org,direct"
  19. GOROOT="/usr/local/go"
  20. GOSUMDB="sum.golang.org"
  21. GOTMPDIR=""
  22. GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
  23. GOVCS=""
  24. GOVERSION="go1.18.3"
  25. GCCGO="gccgo"
  26. GOAMD64="v1"
  27. AR="ar"
  28. CC="gcc"
  29. CXX="g++"
  30. CGO_ENABLED="1"
  31. GOMOD="/home/xxx/files/Programming/go-playground/gin/go.mod"
  32. GOWORK="/home/xxx/files/Programming/go-playground/go.work"
  33. CGO_CFLAGS="-g -O2"
  34. CGO_CPPFLAGS=""
  35. CGO_CXXFLAGS="-g -O2"
  36. CGO_FFLAGS="-g -O2"
  37. CGO_LDFLAGS="-g -O2"
  38. PKG_CONFIG="pkg-config"
  39. 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服务:

目录结构

  1. ./Playground/
  2. ├── go.mod
  3. ├── go.sum
  4. └── main.go

步骤1:初始化模块

  1. go mod init gin
  2. go mod tidy

步骤2:添加gin依赖

  1. go get github.com/gin-gonic/gin

步骤3:创建main.go文件,并将问题中提供的代码复制进去

步骤4:运行

  1. go run ./main.go
英文:

I am able to start http service with following steps

Directory structure

  1. ./Playground/
  2. ├── go.mod
  3. ├── go.sum
  4. └── main.go

Step.1 module init

  1. go mod init gin
  2. go mod tidy

Step.2 Add gin dependency

  1. go get github.com/gin-gonic/gin

Step.3 Create main.go, with code given in question

Step.4 Run

  1. go run ./main.go

huangapple
  • 本文由 发表于 2022年7月14日 05:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/72972764.html
匿名

发表评论

匿名网友

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

确定