在Go中对我的代码进行命名空间处理。

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

Namespacing my code in Go

问题

我正在使用Go编写一个相当简单的应用程序,更多是作为学习工具而不是其他用途。

我真正想做的是对我的代码进行一些组织 - 例如,我已经将一些Hipchat API命令分离到一个单独的文件中。目前,它仍然在main包下,但我更希望将它(命名空间)移动到一个子包中。问题是,Go似乎认为这样的包应该存在于我的$GOPATH中,尽管它与除了正在编写的项目之外的任何项目都无关。

我可能正在尝试错误使用包功能,如果是这样的话,那么实现我想做的事情的最佳方法是什么?

英文:

I'm writing a fairly simple application in Go, more as a learning tool than anything else.

What I'd really like to do is to organise my code somewhat - for example, I've separated out some Hipchat API commands into a separate file. Right now, it's still under the main package, but I'd prefer to move it (namespace it) into a kind of subpackage. The thing is, Go seems to think that such a package should live in my $GOPATH, despite it not being relevant to any project besides the one it's being written for.

I'm probably trying to misuse the package functionality, so if so, what's the best way to achieve what I'm trying to do?

答案1

得分: 1

感谢tkausl在评论中给出的指导,我找到了实现我想要的功能的方法。我将我的项目移动到$GOPATH中,并通过import subpackage from "project/subpackage"访问子包。

如果没有这个帮助,我可能要花一段时间才能找到解决方法!这只是学习Go语言的方式的一个案例。

英文:

Thanks to the pointers from tkausl in comments left on my question, I figured out the way to achieve exactly what I wanted. I moved my project to reside in $GOPATH and then accessed the sub-package via import subpackage from "project/subpackage".

Wouldn't have got there for a while without that help! Just a case of learning Go's way of doing things.

huangapple
  • 本文由 发表于 2016年8月25日 19:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/39144477.html
匿名

发表评论

匿名网友

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

确定