9

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

5
  • 1
    Do not use go run with 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.
    – JimB
    Commented Jul 13, 2022 at 21:23
  • 2
    Guessing here, but go mod init gin could be a name that conflicts with github.com/gin-gonic/gin. Try something like go mod init github.com/your-account/gin. Commented Jul 13, 2022 at 22:20
  • @JimB I tried go build and getting the message directory . outside modules listed in go.work or their selected dependencies. Setting GOWORK=off and trying again hasn't worked.
    – zyros
    Commented Jul 14, 2022 at 6:09
  • @DmitryHarnitski I changed the package name to something else without success
    – zyros
    Commented Jul 14, 2022 at 6:10
  • 1
    Ok, the problem was my original code was inside a Go workspace and I haven't added it to the workspace with go work use'
    – zyros
    Commented Jul 14, 2022 at 6:35

1 Answer 1

6

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
2
  • i followed your steps and it worked. I haven't expected that as the steps are basically the same as in my post but this works strangely. Later this day I'll figure out what the problem is. Thanks
    – zyros
    Commented Jul 14, 2022 at 6:27
  • Ok, the problem was my original code was inside a Go workspace and I haven't added it to the workspace with go work use'
    – zyros
    Commented Jul 14, 2022 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.