如何在go中导入本地包?

我是新手,正在研究要本地化的示例代码。

在原始的main.go导入语句中,它是:

 import (

"log"

"net/http"

"github.com/foo/bar/myapp/common"

"github.com/foo/bar/myapp/routers"

)

现在我已经commonrouters/home/me/go/src/myapp

所以我将import语句转换为:

import (

"log"

"net/http"

"./common"

"./routers"

)

但是当我运行时,go install myapp出现以下错误:

can't load package: /home/me/go/src/myapp/main.go:7:3: local import "./common" in non-local package

另外,当我在导入语句中使用commonand routers代替./commonand ./routers时,得到:

myapp/main.go:7:3: cannot find package "common" in any of:

/usr/local/go/src/common (from $GOROOT)

/home/me/go/src/common (from $GOPATH)

myapp/main.go:8:2: cannot find package "routers" in any of:

/usr/local/go/src/routers (from $GOROOT)

/home/me/go/src/routers (from $GOPATH)

我怎样才能解决这个问题?

回答:

好吧,我发现了问题所在。基本上,导入的开始路径是$HOME/go/src

所以我只需要myapp在包名称前面添加,即导入应该是:

import (

"log"

"net/http"

"myapp/common"

"myapp/routers"

)

以上是 如何在go中导入本地包? 的全部内容, 来源链接: utcz.com/qa/411507.html

回到顶部