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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题