我是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
答案 0 :(得分:2)
♂️
我终于找到了这个问题。在eval
文件中,我必须导入main.go
而不是app/controllers
,因为./controllers
路径为Dockerfile
。