doudaotui4297 2016-06-01 19:44
浏览 115

应用程序与包中的Golang项目结构

My organization uses Rails to develop its app but I'm attempting to re-write one of our back-end processes in Golang because it's much quicker.

I've structure my application with our company a namespace for my app (example.co), and a subfolder for each of the packages within my app.

Each library that I've included (e.g. sqlx, etc...) also has it's own folder.

src/
  github.com/
    jmoiron/
      (sqlx package files)
  example.co
    my_app/
      (my app package files)
    model/
      (model package files...)

However looking at other packages like sqlx, it appears they scrap this directory structure entirely and put all their files in the root directory

Is this because I'm writing an application and sqlx is a package that's meant to be included in other applications? Or is it just a difference in preference since there's no real accepted "standard"

  • 写回答

2条回答 默认 最新

  • duanjiang7505 2016-06-01 20:35
    关注

    I did this too on my first project. I have since learned:

    • the $GOPATH/bin/ pkg/ src/ layout is constructed by go get and similar commands
    • you can organize your .go files as a single flat project dir or with subfolders (caveat: all .go files in the same folder must have the same package name)
    • put other people's code in a /vendor directory inside your project root, if it is code your app needs to work (google this, it's the worst part of go imo)
    • put your own project under your gopath, symlink to it if you want it more accessible

    So I would imagine your code could look something like:

    /Users/user2490003/MyGoPath/
    ▾ src/github.com/user2490003/myproject/
      ▾ model/
          user.go
      ▾ myapp/
          myapp.go
      ▾ vendor/github.com/jmoiron/sqlx/
          sqlx.go
        main.go
    

    Import the full package references, like this:

    // main.go
    package main
    import (
      github.com/jmoiron/sqlx
      github.com/user2490003/myproject/myapp
      github.com/user2490003/myproject/model
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型