如何导入本地软件包? [英] How to import local packages in go?

查看:129
本文介绍了如何导入本地软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在原来的 main.go import语句为:

  import(
log
net / http
github.com/foo/bar/myapp/common
github.com/foo/bar/myapp/routers

现在我有 common 路由器包在 / home / me / go / src / myapp



所以我将import语句转换为:

 导入(
log
net / http
./common
./routers

但是当我运行 go install myapp 我得到这些错误:

 无法加载包:/ home /me/go/src/myapp/main.go:7:3:非本地包中的本地导入./common

此外,当我使用 common 路由器而不是 ./common 。 / routers ,我得到:

  myapp / main.go:7:3 :在
/ usr / local / go / src / common(from $ GOROOT)
/ home / me / go / src / common(from $ GOPATH)$中找不到软件包common
/ usr / local / go / src / routers(from $ GOROOT)
/ home / me中的任何一个都无法找到包路由器:b $ b myapp / main.go:8: / go / src / routers(from $ GOPATH)

我该如何解决这个问题?

解决方案

好的,我找到了问题所在。
基本上输入的开始路径是 $ HOME / go / src



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

  
log
net / http
myapp / common
myapp / routers


I am new to go and working on an example code that I want to localize.

In the original main.go import statement it was:

 import (
    "log"
    "net/http"
   "github.com/foo/bar/myapp/common"
    "github.com/foo/bar/myapp/routers"
)

Now I have common and routers package in /home/me/go/src/myapp

So I converted the import statement to:

import (
    "log"
    "net/http"
    "./common"
    "./routers"
)

But when I run go install myapp I get these errors:

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

Also, when I use common and routers instead of ./common and ./routers in the import statement, I get:

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)

How can I fix this?

解决方案

Well, I figured out the problem. Basically Go starting path for import is $HOME/go/src

So I just needed to add myapp in front of the package names, that is, the import should be:

import (
    "log"
    "net/http"
    "myapp/common"
    "myapp/routers"
)

这篇关于如何导入本地软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆