Search code examples
gitgoconventions

Project Structure and Committing golang projects


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.


Solution

  • 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.