dongse5408 2018-11-30 17:20
浏览 461
已采纳

使用git设置正确的Golang目录结构,以在自定义包上使用go build

So I've been scratching my head at this for a couple weeks now, and after reading several source on how $ go build works and its three magic directories, /bin, /pkg, /src, it's still not very clear to me how to build Golang projects using custom packages and how a git repo should be managed.

Let me explain my situation in more detail:

I'm working on a Go project in a project directory that is not the same as the default .../user/go/.... For all my projects, I have a different directory tree that is structured like this:

projects
 | project-a
 | project-b
    | docs
    | media
    | scrum
    | project-b   <-- git repo containg Go structure
       | .git
       | gitignore.txt
       | bin
       | pkg
       | src
          | custom-package-a
          |  | foo.go
          | custom-package-b
          |  | bar.go
 |     | main.go      
 | project-c

My projects directory could contain any kind of project: java, Unity3D, VisualC#, etc... then in each project under that it contains a repo with the source code.

I've been able to build successfully recently by adding projects/project-b/project-b to my GOPATH so it sees the src directory.

Is this how the file structure for a typical Go project should look, even though this builds correctly?

In my GOPATH, I removed the original path to user/go and just used the project directory only. When installing other packages from github, they get included in the repo because the GOPATH isn't set anywhere else, so my repo has these different submodules. Is installing external packages into a repo a wise thing to do, or should those go into a different go directory? I'm worried it might muddy the repo.

I can include

I need to know if I'm using custom packages correctly. My intent is to take an object oriented approach with my code base and I'm treating each packages as a class. Is making custom packages to treat them as classes a smart thing to do? I found it necessary to avoid same-name conflicts between functions and variables. Example: package-a.GetThing(), package-b.GetThing(). Both functions yield a similar (not exact) output, but are working with different sets of data and require different implementation.

My console is in projects/project-b/project-b/ when I use go build and it works correctly. Same if I move main.go inside src.

One problem is that the Go builder is strangely placing the compiled binary file in the same directory I invoke go build from. Shouldn't go build be placing it in the bin directory, or do I need to enforce the output path when using the command? I'm aware of GOBIN, but it doesn't seem to be working.

  • 写回答

1条回答 默认 最新

  • doucai6663 2018-11-30 22:08
    关注

    As of Go 1.11 you don't need to use $GOPATH.

    You can use go mod init [your repo] and just run go install or go build The deps will be downloaded for you and a go.mod and go.sum files will be created to track deps.

    Check out this README

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

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛