GoLang和带有相对路径的软件包布局
I'm learning Go language and I've made this example: http://thenewstack.io/make-a-restful-json-api-go/ to build a simple REST API.
I've compiled it and all works fine but all sources are in the main package.
Now I want to organize my .go files in packages, so I move them into some folders this way:
GOPATH\bin
GOPATH\pkg
GOPATH\src\pack1\Handlers.go
GOPATH\src\pack1\Logger.go
GOPATH\src\pack1epo.go
GOPATH\src\pack1\Todo.go
GOPATH\srcouter\Router.go
GOPATH\srcouter\Routes.go
GOPATH\src\Main.go
Main.go uses all the router package so I've put in the import section: "./router". Router.go uses the pack1 package, so in Router.go I've imported "../pack1". Now if I try to "go build Main.go" I get:
router\Router.go:6: imported and not used: "_/D_/GOPATH/src/pack1"
router\Router.go:14: undefined: Logger
and similar errors, so seems that the import of pack1 package that I've made, it's wrong. Of course in all files belonging to pack1, in the header I've put the "package pack1" definition.
I've also read that the relative imports are not suggested in Go and it could be useful to use the remote packages such ad importing "github.com/myrepo/mypackage". But I dont want to use remote imports; I want to push all my files in a second moment.
Could you help me to better understand what is the way to have local imports between packages in Go language?
thanks in advance
我正在学习Go语言,并以以下示例为例: http://thenewstack.io/make-a-restful-json-api-go/ to 构建一个简单的REST API。 p>
我已经对其进行了编译,并且一切正常,但是所有资源都在主包中。 p>
现在,我想 将我的.go文件整理成包,因此我将它们移动到某些文件夹中: p>
GOPATH \ bin
GOPATH \ pkg
GOPATH \ src \ pack1 \ Handlers.go \ nGOPATH的\ src \ PACK1 \ Logger.go
GOPATH的\ src \ PACK1
epo.go
GOPATH的\ src \ PACK1 \ Todo.go
GOPATH的\ src \路由器\ Router.go
GOPATH的\ src \路由器\ Routes.go \ nGOPATH \ src \ Main.go
code> pre>
Main.go使用了所有路由器软件包,因此我将其放在了import部分:
“ ./ router”。
Router.go使用pack1包,因此在Router.go中,我导入了“ ../pack1”。
现在,如果我尝试“去构建Main.go”,我会得到: p>
router \ Router.go:6:导入但未使用:“ _ / D_ / GOPATH / src / pack1”
router \ Ro uter.go:14:未定义:Logger
code> pre>
和类似的错误,所以看来我制作的pack1软件包的导入是错误的。 当然,在所有属于pack1的文件中,在标题中都放置了“ package pack1”的定义。 p>
我还阅读了Go中不建议使用相对导入的内容, 使用远程软件包(例如广告导入“ github.com/myrepo/mypackage”)可能会很有用。
但是我不想使用远程导入; 我想稍后再推送所有文件。 p>
您能不能帮助我更好地了解使用Go语言在软件包之间进行本地导入的方式是什么? p>
请先感谢 p>
div>
The reason you're not supposed to use relative imports is that they don't work inside of $GOPATH
. Get rid of the "relative" part of your import paths, since all imports are relative to $GOPATH/src
import (
"pack1"
"router"
)
You will also want to move your Main.go
into a package as well, so you you can make full use of the go tools.
As JimB says, read through the 'How to write go code' link, specifically https://golang.org/doc/code.html#ImportPaths
- All packages exist inside a directory tree starting at $GOPATH/src.
- Import path is effectively the full path to the package (from 1)
- There are no sub packages in GO.
- main packages are used to create commands
So in your case, your import paths will be "pack1" and "router"