doushadu0901 2016-04-09 20:59
浏览 20
已采纳

Web应用程序的Go Lang文件夹结构

As you know Go is a modern approach to OOP imo, with brilliant things like forcing you to use composition over inheritance. I am just trying to understand how a well written go code has to be designed in terms of folder and package structure.

I am just wondering if this React like approach which can be seen below - dividing app as components to different packages can work?

Or can you give me an example, link, idea for a good structure in terms of folder/packaging of a web api. Go will only be an api in the background to my open source project, on client side I am planing to have a single-page React app btw.

Thanks a lot,

enter image description here

  • 写回答

1条回答 默认 最新

  • dongzengzai4567 2016-04-10 04:58
    关注

    I tend to structure my apps as: $GOPATH/github.com/yourname/projectname/ cmd/ app1/ main.go app2/ main.go db/ 001_initial_schema.sql 002_add_timestamps.sql ... etc ... lib/ lib1/ lib2/ html/ ..all the html stuff.. where app1 / app2 are the command line apps. Often it is only be one app (your web server).

    lib/* is just whatever segregated pieces of functionality you have.

    Usually, i start with just cmd/app1 and then expand in to lib when the project gets sufficiently complex.

    And make your static file server (assuming you have one), use "html" as the directory.

    For database migrations, I use a really simple migrator I wrote as I find the others to be too complicated / too large.

    Here's the code I'm using in my projects.
    I thought of making it a real library, but I'm pretty sure this is postgresql specific as it assumes that DDL is transactional.

    With this structure, you can simply do (from the project root):

    go install ./... && app1

    To build / test your app.

    This structure will also naturally work if you then want to deploy to Heroku as heroku sets your working dir to your project root.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据