dongmei9508 2018-10-04 20:14
浏览 144

如何为多个Go项目组织代码?

So, I have a few web services written in Go and I am planning to write a few more. The services all live in their own folders outside of GOPATH and use Glide and Dep for managing vendored dependencies (I intend to convert to Go 1.11 modules in the near future).

The problem I am having is that there is a lot of code duplication in these services, making things fragile. Especially code related to database structure and access. I would like to move all the database access and other service specific code into a shared location somewhere and import it into the services as needed.

What is the right way to do that? I have been experimenting with different project structures, but I always run into one of the two problems.

If I use Go 1.11 modules, I end up having to share go.mod and go.sum between all the services. This makes the build process for individual services longer, since each service build has to fetch all of the dependencies from all the services in the same location. As my number of services will grow, this problem will become worse.

If I set GOPATH so my service code ends up inside, I run into multiple problems. The services are part of a larger project written in a different language and are part of the version control repository for the project. The structure, currently, is

- project
    - services
        - svc1
        - svc2
    - othercode
    - morecode

If I set the GOPATH to services, I have to move individual services directories under a separate src, I end up with extra pkg and bin directories and every Go package I ever want to install ends up somewhere in the ~/project/services tree as well. I would rather keep using vendored dependencies.

How do I organize my code, so I avoid either of the above scenarios and still be able to import shared code from multiple services?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
    • ¥15 画两个图 python或R