英文:
How to add go code into an existing project
问题
我对Go语言还不太熟悉。我们目前正在将一些微服务从我们的单体Django+Python Web应用中拆分出来,我们决定用Go来实现其中的一部分。问题是这些服务的源代码应该与主应用程序存放在同一个代码库中。所以我把所有的Python代码放在了~/GloriousMomolith/thedjangoapp目录下,将拆分出来的服务放在了~/GloriousMomolith/services/some-service-name目录下。
我可以将~/GloriousMonolith移动到~/src目录下(我将$GOPATH设置为$HOME),但这样每次引用我创建的Go包时,我都需要写import GloriousMomolith/services/someservice/somepackage
。我想避免这种情况。至少,我想避免在任何地方硬编码GloriousMomolith
这部分。有什么建议吗?
英文:
I am pretty new to go. We are currently splitting some microservices off of our monolithic Django+python web app and we have decided to do at least some of them in go. The problem is that the sources for the services are supposed to live in the same repo as the main app. So I have all the python code in ~/GloriousMomolith/thedjangoapp and split off services in ~/GloriousMomolith/services/some-service-name.
I could move ~/GloriousMonolith under ~/src (I have $GOPATH set to $HOME), but then every time I refer to a go package I create I would have to do import GloriousMomolith/services/someservice/somepackage
. I want to avoid that. At the very least, I want to avoid having GloriousMomolith
part hard-coded anywhere. Any suggestions?
答案1
得分: 1
你可以将一个Go源代码目录添加到你的项目中。例如:
~/
GloriousMomolith/
thedjangoapp/
src/
services/
someservice/
service.go
将GOPATH
设置为$HOME/GloriousMomolith:$HOME
。
现在你可以相对于src
目录进行导入:
import (
"services/someservice"
)
英文:
You can add a Go source directory to your project. For example:
~/
GloriousMomolith/
thedjangoapp/
src/
services/
someservice/
service.go
Set GOPATH
to $HOME/GloriousMomolith:$HOME
.
You can now import relative to the the src
directory:
import (
"services/someservice"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论