$GOPATH目录和磁盘空间

huangapple go评论78阅读模式
英文:

$GOPATH directory and diskspace

问题

在使用GO语言开发了一两年后,我查看了一下我的$GOPATH目录大小,惊讶地发现它已经增长到了4GB。

我知道磁盘空间应该是廉价的(而且在这台128GB的笔记本SSD上并不算多),但还是有点吃惊。

所以我的问题是,有没有一些好的做法来管理$GOPATH目录的大小?

重新从头开始是否是一个好主意?(尽管可能非常耗时)

英文:

after a year or two developing with GO I had a look at my $GOPATH directory size and was surprised to see it was growing to a 4GB.

I understand that disk space is supposed (and it's not that much on this 128GB laptop SSD) to be cheap but still.

So my questions is, is there some good practice to manage the size $GOPATH directory ?

Would restarting from scratch would be a good idea? (although could be very much time consuming)

答案1

得分: 3

你的陈述中提到删除GOPATH可能会耗费时间,听起来像是你正在使用普通的go get ...来管理Go项目的依赖关系。在我的环境中,我有两种方法使GOPATH临时化。

  1. 不要使用普通的go get来管理依赖关系。Go 1.6引入了/vendor目录。结合像Glide这样的依赖管理工具,每个项目的所有依赖都位于项目目录中。

    1. 这意味着,如果你不再需要某个依赖项,你可以清空项目的vendor目录并重新下载依赖项。因此,你的磁盘上只会有你实际需要的依赖项。

    2. 此外,如果你停止工作并从磁盘中删除一个项目,依赖项也会被删除。

  2. 你可以在GOPATH中指定多个路径。就像PATH环境变量一样,你可以用冒号分隔它们。有趣的是,Go使用第一个路径来下载项目。在我的机器上,GOPATH看起来像$HOME/.gopath:$HOME/projects。因此,如果你把所有实际的项目放在第二个目录中,你就可以清除第一个目录,但不需要担心必须重新克隆每个项目。

这两个方法不能帮助减少Go项目及其依赖项的磁盘使用量,但可以更好地了解实际所需的内容,并使你能够随时删除依赖项目录。

英文:

Your statement, that deleting the GOPATH would be time consuming, sounds like you are managing the dependencies of your Go projects with plain go get .... In my environment I do two things to make the GOPATH ephemeral.

  1. Don't use plain go get for dependency management. Go 1.6 introduced the /vendor directory. Together with a dependency management tool like Glide every dependency for a certain project lies withing the project directory.

  2. This means, if don't need a dependency anymore you can wipe the vendor directory of the project and download the dependencies again. Therefore you only have dependencies on your disk, which you actually need.

  3. Also, if you stop working on a project and delete it from your disk, the dependencies also get deleted.

  4. You can specify multiple paths in the GOPATH. Like the PATH environment variable you can split them by a colon. The interesting thing is, that Go uses the first path for downloading projects. On my machine the GOPATH looks like $HOME/.gopath:$HOME/projects. So if you put all your actual projects into the second directory, you will have an clear separation between your projects and dependencies. Thus you could wipe the first directory from time to time, but don't need to fear that you have to clone every of your projects, again.

These two things don't help to reduce the disk usage of your Go projects and their dependencies, but you get a better overview whats actually needed and makes you able to delete the dependency directory whenever you like.

huangapple
  • 本文由 发表于 2017年2月18日 15:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/42312185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定