GO – 本地导入不起作用

huangapple go评论90阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年5月24日 14:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/44150559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定