How to add go code into an existing project

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

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"
)

huangapple
  • 本文由 发表于 2017年6月7日 01:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/44395946.html
匿名

发表评论

匿名网友

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

确定