-3

I'm learning Go ad I'm trying to build go file:

package main

import (
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
    "net/http"
)

func main() {
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("welcome"))
    })
    http.ListenAndServe(":3000", r)
}

But when I build the program with the command go build main.go, it outputs:

go: github.com/go-chi/chi/@v1.5.4: missing go.sum entry; to add it:
        go mod download github.com/go-chi/chi/

go.mod:

module exprog

go 1.16

require github.com/go-chi/chi/ v1.5.4

when I execute go mod download github.com/go-chi/chi/, I get this error:

go: github.com/go-chi/chi/@v1.5.4: malformed module path "github.com/go-chi/chi/": trailing slash

What I should do?

Vad Sim
  • 266
  • 8
  • 21
  • 5
    don't try to build individual go files, build the package. I'm not sure how you got `go mod download github.com/go-chi/chi/`, but you should be using `go get github.com/go-chi/chi/v5` – JimB Sep 27 '21 at 14:30
  • I tried, it doesn't work – Vad Sim Sep 27 '21 at 14:33
  • 1
    You tried what exactly? Please create a [mre], because the single [go source you have](https://play.golang.org/p/z332IlxMjMe) will not produce this error. – JimB Sep 27 '21 at 14:40

2 Answers2

2

It's error not in command, it's error in go.mod file. You can fix by:

module exprog

go 1.16

require github.com/go-chi/chi/v5 v5
-1

Assuming you're looking to download this module, You should do go mod download github.com/go-chi/chi/v5. The name of the module is the header in the dependency's go.mod file.

You can remove the entry from go.mod and simply do go mod download github.com/go-chi/chi/v5

or

You can remove the entry from go.mod, and do go mod tidy. Go will fill your go.mod file based on your imports. You can do go mod download then.

  • 1
    Go will automatically run `go mod tidy` for you if you initialize a module `with go mod init ....`. – erik258 Sep 27 '21 at 14:53
  • The previous comment adds to the answer but it does not explain a downvote. –  Sep 27 '21 at 21:56