进行深度检查-进行深度检查后,缺少子软件包“ chi /中间件”

时间:2019-04-21 04:03:16

标签: go go-chi

运行dep sure后,从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上,它缺少其子包。

1 个答案:

答案 0 :(得分: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                  

go mod init将协调您的dep Gopkg.lock文件。
并且它应该检测并获取您缺少的依赖项。
如果可行,请运行go mod tidy来修剪任何多余的要求。