英文:
GO - Local import not working
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是golang的新手,想要构建我的第一个简单的Web应用程序。每次我想要构建我的源代码时,都会出现错误在非本地包中导入“./controllers”
。
这是我的文件夹结构:
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("/", controllers.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构建命令:
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
希望对你有帮助!如果你有任何其他问题,请随时问我。
英文:
I'm new with golang and I want to build my first simple web application. Every time I want to build my source, I get an error locale import "./controllers" in non-local package"
.
Here is my folder structure:
goTest
├── Dockerfile
├── controllers
│   └── IndexController.go
└── main.go
The main.go
file:
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))
}
The IndexController.go
file:
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:])
}
My Dockerfile
file (copied from 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"]
The docker and go build command:
docker build -t my-golang-app .
And at least the console output:
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
得分: 4
我终于找到问题了。在main.go
文件中,我必须导入app/controllers
而不是./controllers
,因为在Dockerfile
中路径是/go/src/app
。
英文:
🤦♂️
I finally found the issue. In the main.go
file I have to import app/controllers
instead of ./controllers
because in the Dockerfile
the path is /go/src/app
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论