项目结构和提交golang项目

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

Project Structure and Committing golang projects

问题

简而言之,Go语言项目的命名方式中包含网站路径的原因是为了方便项目的版本控制和共享。这种命名约定使得项目可以在不同的代码托管网站上进行托管和共享,比如GitHub、GitLab等。通过将项目的路径与网站相关联,可以更容易地找到和访问项目的代码库。

在这种命名约定下,你可以将项目的代码库初始化为一个Git仓库。通常情况下,你可以在项目的根目录下初始化Git仓库,这样可以跟踪整个项目的变化。你可以使用命令行工具或者图形化界面工具来初始化Git仓库,并将代码提交到仓库中进行版本控制。

总结一下,将网站路径包含在Go项目的命名中是为了方便项目的版本控制和共享,你可以在项目的根目录下初始化Git仓库来进行版本控制。希望这个解释对你有帮助!

英文:

TL;DR: Why do I name go projects with a website in the path, and where do I initialize git within that path? ELI5, please.

I'm having a hard time understanding the fundamental purpose and use of the file/folder/repo structure and convention of projects/apps in the go language. I've seen a few posts, but they don't answer my overarching question of use/function and I just don't get it. Need ELI5 I guess.

Why are so many project's paths written as:

github.com/project1/goLangProj
gitlab.com/project2/goLangProj2
example.com/hello

Is there a reason why the start of the project repo is a website? I kind of get that maybe there are multiple projects and they are all going to one of those repo hosting sites, but I don't fully get the purpose otherwise. And it confuses me as to when I want to commit a project and the project is then named with that in the repository, no? I'm somewhat looking for a use case beyond the committing of the project as well, as in someone that wants to download and use the project too, both perspectives.

In addition to the reasoning and purpose, if we get that far, then at which folder within that path would I initialize the git project?

If there is a full explanation I may have just not been able to find, please share it with me and I'll move on to that, Thank you in advance.

答案1

得分: 1

为什么我在项目路径中使用网站命名项目?

如果你的包的导入路径与其他人的包完全相同,那么在同一个项目中使用这两个包将会很困难,因为导入路径不是唯一的。只要每个人都使用一个与他们拥有的 URL 相等的字符串,比如你的 GitHub 账户(或者实际拥有的域名),那么这些命名冲突就不会发生(除非 URL 的所有权随时间改变)。

这样命名还可以更容易地使用 go get 命令获取你的项目,因为主机位置是导入字符串的一部分。每个使用该包的源文件也会告诉你从哪里获取它。这是一个很好的特性。

我应该在哪里初始化 git?

你的项目应该有一个包含项目中所有内容且不包含项目外部内容的根文件夹。在这个目录中初始化 git。如果是 Go 项目,通常也会在这里初始化你的 Go 模块。

你可能会受到代码托管位置的限制,限制你放置 git 根目录的位置。例如,如果在 GitHub 上托管代码,你推送的所有代码都必须放在一个仓库内。这意味着你可以将 git 根目录放在包含所有仓库的更高级目录中,但我不知道有没有办法将其推送到远程仓库(如果有的话)。请记住,你的本地文件系统与远程主机的文件系统不同。你可能有一个名为 github.com/myname/ 的本地文件夹,但这并不意味着远程端支持将文件写入这样的位置。

英文:

> Why do I name projects with a website in the path?

If your package has the exact same import path as someone else's package, then someone will have a hard time trying to use both packages in the same project because the import paths are not unique. So long as everyone uses a string equal to a URL that they effectively "own", such as your GitHub account (or actually own, such as your own domain), then these name collisions will not occur (excepting the fact that ownership of URLs may change over time).

It also makes it easier to go get your project, since the host location is part of the import string. Every source file that uses the package also tells you where to get it from. That is a nice property to have.

> Where do I initialize git?

Your project should have some root folder that contains everything in the project, and nothing outside of the project. Initialize git in this directory. It's also common to initialize your Go module here, if it's a Go project.

You may be restricted on where to put the git root by where you're trying to host the code. For example, if hosting on GitHub, all of the code you push has to go inside a repository. This means that you can put your git root in a higher directory that contains all your repositories, but there's no way (that I know of) to actually push this to the remote. Remember that your local file system is not the same as the remote host's. You may have a local folder called github.com/myname/, but that doesn't mean that the remote end supports writing files to such a location.

huangapple
  • 本文由 发表于 2021年6月16日 10:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/67995562.html
匿名

发表评论

匿名网友

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

确定