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
go runwith filenames, nothing in the official docs suggests to do this. That however is not the actual problem here, there is something you haven't shown us, because your example would still work as presented.go mod init gincould be a name that conflicts withgithub.com/gin-gonic/gin. Try something likego mod init github.com/your-account/gin.go buildand getting the messagedirectory . outside modules listed in go.work or their selected dependencies. Setting GOWORK=off and trying again hasn't worked.go work use'