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

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

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 gingo 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

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:

确定