一些开源项目以及公司、个人项目,都被我扔进了GoPath下的src目录,看起来杂乱无章,那么如何使项目的目录层级显得更优雅?

image.png

如何解除对 GOPATH 的依赖

开启go module之前,代码许需要放在$GOPATH/src/目录下
开启go module之后,代码可以放在任意位置

如何在项目中引用github上的包

  1. D:\he11c\Go\study\目录下新建项目:go_module_demo
  2. 创建go.mod文件

go mod init github.com/elpsyr/go_module_demo

  1. 这里我们想要使用github上的某款基于go实现的web框架,以gin为例

go get -u github.com/gin-gonic/gin

  1. 接着我们在import中引用,就就可以在代码中使用gin实现的功能了

如何将项目作为一个module引用

参考:https://github.com/elpsyr/go_module_demo

References

  • 关于go module

https://zhuanlan.zhihu.com/p/59549613
https://www.cnblogs.com/mayanan/p/15487097.html