本文介绍了Docker golang包导入错误:导入路径不以主机名开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试码头和去项目。这是我的码头文件

  FROM golang 

ARG app_env
ENV APP_ENV $ app_env

COPY ./ /go/src/github.com/user/myProject/app
WORKDIR /go/src/github.com/user/myProject/app



RUN go get ./
RUN go build

如果[$ {APP_ENV} =生产]; CMD; \
然后\
app; \
else \
go get github.com/pilu/fresh&& \
新鲜; \
fi
EXPOSE 8080

运行良好。然后我在我的go程序中添加了一个testpack包。

  package main 

import(
fmt
time
testpack


var now = time.Now()
var election = time。日期(2016,time.November,8,0,0,0,time.UTC)
func main(){
//获取选举日期和现在之间的期限
untilElection: = election.Sub(now)
//以毫微秒为单位获得持续时间
toNanoseconds:= tillElection.Nanoseconds()
//计算小时到纳秒
小时:= toNanoseconds / 3600000000000
余数:= toNanoseconds%3600000000000
//从剩余小时数中得出分钟数
分钟数:=余数/ 60000000000
余数=余数%60000000000
//从余数中派生秒分钟
秒:=剩余/ 1000000000
//计算天数并获得剩余小时数
天:=小时/ 24
hoursLeft:=小时%24

fmt.Printf(\\\
直到2016年美国总统大选?\\\\\\%天,%,小时,%,分钟, $ p>

现在我运行=> docker build ./



我收到错误



包testpack:无法识别的导入路径testpack(导入路径不以hostname开头)

我试过这个,但无法解析



任何帮助都是值得赞赏的。

解决方案

很明显,它试图从Internet上加载它,因为它没有在你的GOPATH中找到testpack。



向我们展示您的GOPATH设置或您将测试包复制到的位置,除了说我错过了这些我就可以告诉您。 HTTPS ://golang.org/cmd/go/#hdr-Relative_import_pathsrel =nofollow noreferrer> https://golang.org/cmd/go/#hdr-Relative_import_paths



试试


  • import./testpack\"

  • 将Dockerfile中的GOPATH设置为/ go

    importgithub.com/user/myProject/app/testpack / code>


I am trying to test docker and go project. Here is my dockerfile

FROM golang

ARG app_env
ENV APP_ENV $app_env

COPY ./ /go/src/github.com/user/myProject/app
WORKDIR /go/src/github.com/user/myProject/app



RUN go get ./
RUN go build

CMD if [ ${APP_ENV} = production ]; \
then \
app; \
else \
go get github.com/pilu/fresh && \
fresh; \
fi  
EXPOSE 8080

It runs fine. Then i added a package "testpack" to my go program.

package main

import(
"fmt"
"time"
"testpack"
)

var now = time.Now()
var election = time.Date(2016, time.November, 8, 0, 0, 0, 0, time.UTC)
func main() {
 //get duration between election date and now
tillElection := election.Sub(now)
//get duration in nanoseconds
toNanoseconds := tillElection.Nanoseconds()
//calculate hours from toNanoseconds
hours := toNanoseconds/3600000000000
remainder := toNanoseconds%3600000000000
//derive minutes from remainder of hours
minutes := remainder/60000000000
remainder = remainder%60000000000
//derive seconds from remainder of minutes
seconds := remainder/1000000000 
//calculate days and get hours left from remainder
days := hours/24
hoursLeft := hours%24

fmt.Printf("\nHow long until the 2016 U.S. Presidential election?\n\n%v Days %v Hours %v Minutes %v Seconds\n\n", days, hoursLeft, minutes, seconds)

}

Now i ran=> docker build ./

I am getting an error

package testpack: unrecognized import path "testpack" (import path does not begin with hostname)

I tried this Error 'import path does not begin with hostname' when building docker with local package but couldn't resolve

Any help is appreciated.

解决方案

It is obviously trying to load it from the Internet because it isn't finding "testpack" in your GOPATH.

You didn't show us your GOPATH setting or where you copied "testpack" to, so other than saying "It's missing" that's all I can tell you.

Read https://golang.org/cmd/go/#hdr-Relative_import_paths

Try either

  • import "./testpack"
  • Set GOPATH to "/go" in your Dockerfile
    import "github.com/user/myProject/app/testpack"

这篇关于Docker golang包导入错误:导入路径不以主机名开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 11:14