2

运行 dep ensure 后缺少 go-chi 的中间件子包。

project-backend     |  main.go:8:2: cannot find package "github.com/go-chi/chi/middleware" in any of:
project-backend     |   /go/src/backend/vendor/github.com/go-chi/chi/middleware (vendor tree)
project-backend     |   /usr/local/go/src/github.com/go-chi/chi/middleware (from $GOROOT)
project-backend     |   /go/src/github.com/go-chi/chi/middleware (from $GOPATH)

我的 Docker 文件,我还在 docker-compose.yaml 文件中安装了我的代码卷

FROM golang:1.12

WORKDIR /go/src/backend
COPY Gopkg.toml Gopkg.lock ./
RUN go get -u github.com/golang/dep/cmd/dep
COPY . ./
RUN dep ensure

RUN go get github.com/pilu/fresh

CMD [ "fresh" ]

Gopkg.toml


[prune]
  go-tests = true
  unused-packages = true
  non-go = true

[[constraint]]
  name = "github.com/go-chi/chi"
  version = "4.0.2"

我的代码,只是一个基本的路由器来测试我的 docker 和 dep 配置。

package main

import (
    "fmt"
    "net/http"

    "github.com/go-chi/chi"
    "github.com/go-chi/chi/middleware"
)

func main() {
    r := chi.NewRouter()
    r.Use(middleware.Logger)

    port := ":8080"

    r.Get("/api", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("API is working!"))
    })

    format := "\033[1;36m%s\033[0m"
    fmt.Printf(format, "Build success, running on port "+port)
    http.ListenAndServe(port, r)
}

我的代码在使用 go get 时运行良好。但是在 dep 上,它缺少它的子包。

4

1 回答 1

1

由于您使用的是 Go 1.12,您能否尝试查看使用go mod(Go 1.11+ Modules)时问题是否仍然存在。

$ export GO111MODULE=on                         # manually active module mode
$ cd $GOPATH/src/<project path>                 # e.g., cd $GOPATH/src/you/hello
$ go mod init                  

Ago mod init将协调您的 depGopkg.lock文件。
它应该检测并获取您丢失的依赖项。
如果这有效,那么运行go mod tidy可能会修剪任何无关的要求。

于 2019-04-21T05:13:58.983 回答