Can't import gorilla/mux (github.com/gorilla/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt)
Asked Answered
B

4

11

In Go, I was trying simple database connection. I need to import gorilla/mux, but I couldn't.

I am using VS Code. After cd ing to my project directory, I created main.go and did run go get -u github.com/gorilla/mux

Here is main.go

package main

import (
    "database/sql"
    "fmt"
    "github.com/gorilla/mux"
    _ "github.com/lib/pq"
)

const (
    host     = "localhost"
    port     = 3000
    user     = "postgres"
    password = "postgres"
    dbname   = "test1"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)

    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
      panic(err)
    }

    fmt.Println("Successfully connected!")
}

[Note that, after executing go get -u github.com/gorilla/mux terminal shows

C:\Go\src\github.com\IamFrost\go-rest-api>go get -v -u github.com/gorilla/mux
go: golang.org/x/text upgrade => v0.3.2
go: golang.org/x/crypto upgrade => v0.0.0-20200429183012-4b2356b1ed79
go: golang.org/x/sys upgrade => v0.0.0-20200430082407-1f5687305801
go: golang.org/x/net upgrade => v0.0.0-20200425230154-ff2c4b7c35a0
go: downloading golang.org/x/sys v0.0.0-20200430082407-1f5687305801
golang.org/x/sys/cpu
golang.org/x/crypto/chacha20poly1305
crypto/tls

]

enter image description here Look i have no other syntax error in mini map. At red mark, when I put mouse, the hover text, its funny to me:

1)imported but not used

but the next line

2)no package for import github.com/gorilla/mux)

lol didn't that go against 1) ?

Someone please explain why that occurs

However,

After using go build in terminal

Here is terminal:

C:\Go\src\github.com\IamFrost\go-rest-api>go build
go: inconsistent vendoring in C:\Go\src:
        github.com/gorilla/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/lib/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/[email protected]: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
        golang.org/x/[email protected]: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
        golang.org/x/[email protected]: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod

run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory

[Note : I did 'go mod vendor' also, but no change]

So someone point me why I can't import gorilla/mux or pq.

What else I have to do?

(and please explain what does that mean? is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt)

Baku answered 30/4, 2020 at 9:39 Comment(4)
You can either vendor your dependencies or not vendor them but your current state uses vendored packages and the unvendored gorilla/mux and that is the error message telling you. Re-vendor everything.Comedietta
@Comedietta how to do that? Any example or suggestion?Baku
go mod vendor?Comedietta
@Comedietta i did, but no changeBaku
B
1

I don't know why it works (I am beginner). I found something in net, somehow it worked . Here is how :

  1. This is my go directory C:\Go\ . But I create the project in my desktop folder

  2. Then I recreated the project under C:\Go\src (this is exact folder C:\Go\src\github.com\[github_username]\[project_name] )

  3. Then I pasted all the code at main.go

  4. Finally from terminal go mod vendor

  5. Then go build

  6. Then go run main.go

  7. Finally it worked

[I would like to know why it worked. I would really appreciate your comment : about why they are forcing me to go under C:\Go\src. I already have GOPATH C:\Go\ , GOBIN C:\Go\bin set in environment variable]

Baku answered 12/5, 2020 at 15:58 Comment(0)
S
11

Here is how I fixed it for me:

go get ./...
go mod tidy

If your Go version is less than 1.13 then, you should prepend GO111MODULE=on in above commands.

Sternutation answered 21/4, 2021 at 11:57 Comment(0)
B
1

I don't know why it works (I am beginner). I found something in net, somehow it worked . Here is how :

  1. This is my go directory C:\Go\ . But I create the project in my desktop folder

  2. Then I recreated the project under C:\Go\src (this is exact folder C:\Go\src\github.com\[github_username]\[project_name] )

  3. Then I pasted all the code at main.go

  4. Finally from terminal go mod vendor

  5. Then go build

  6. Then go run main.go

  7. Finally it worked

[I would like to know why it worked. I would really appreciate your comment : about why they are forcing me to go under C:\Go\src. I already have GOPATH C:\Go\ , GOBIN C:\Go\bin set in environment variable]

Baku answered 12/5, 2020 at 15:58 Comment(0)
N
0

In the recent releases of Golang (1.11 onwards I think) there's been a change on the GOPATH variable. My understanding is you only need to set this if GOPATH will not be $HOME/go.

Go looks for your projects in $HOME/go/src/, for instance my Golang projects are in $Home/go/src/github.com/<your_github_userid>` directory, etc.

If you want to use a different location of your projects, you can still set the GOPATH to a different directory.

And my shell file has the following to accommodate both a set and not set GOPATH.

export PATH="$PATH:$(go env GOPATH)/bin"

You can get more information at the GOPATH environment documentation

Neona answered 22/10, 2020 at 3:42 Comment(0)
O
0

go mod init , go mod tidy , go run :3

I working in default directory (C:\Go\src[projectName]) and run commands in project directory. If is not help u , move project to your GOPATH

Oneiric answered 8/7, 2022 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.