Home > Blockchain >  Golang: no required module provides package
Golang: no required module provides package

Time:07-14

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

CodePudding user response:

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
  •  Tags:  
  • go
  • Related