GO - 本地导入无效

时间:2017-05-24 06:29:23

标签: docker go

我是golang的新手,我想构建我的第一个简单的Web应用程序。每次我想构建我的源代码时,都会收到错误locale import "./controllers" in non-local package"

这是我的文件夹结构:

goTest
├── Dockerfile
├── controllers
│   └── IndexController.go
└── main.go

main.go文件:

package main

import (
    "github.com/julienschmidt/httprouter"
    "net/http"
    "log"
    "./controllers"
)

func main() {
    router := httprouter.New()
    router.GET("/", IndexController)

    log.Fatal(http.ListenAndServe(":8080", router))
}

IndexController.go文件:

package controllers

import (
  "fmt"
)

func IndexController(w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
    fmt.Fprintf(w, "Hi %s!", r.URL.Path[1:])
}

我的Dockerfile文件(从https://hub.docker.com/_/golang/复制):

FROM golang:1.8

WORKDIR /go/src/app
COPY . .

RUN go-wrapper download   # "go get -d -v ./..."
RUN go-wrapper install    # "go install -v ./..."

EXPOSE 8080

CMD ["app"]

docker和go build命令:

docker build -t my-golang-app .

至少控制台输出:

Sending build context to Docker daemon  4.608kB
Step 1/7 : FROM golang:1.8
 ---> b2063493320d
Step 2/7 : WORKDIR /go/src/app
 ---> Using cache
 ---> 86d4df9fceaa
Step 3/7 : COPY . .
 ---> 282286e66939
Removing intermediate container fe3c0686729a
Step 4/7 : RUN go-wrapper download   # "go get -d -v ./..."
 ---> Running in f34b5a5ef0de
+ exec go get -v -d
github.com/julienschmidt/httprouter (download)
main.go:7:5: local import "./controllers" in non-local package
The command '/bin/sh -c go-wrapper download   # "go get -d -v ./..."' returned a non-zero code: 1

1 个答案:

答案 0 :(得分:2)

♂️

我终于找到了这个问题。在eval文件中,我必须导入main.go而不是app/controllers,因为./controllers路径为Dockerfile