在“如何编写Go代码”示例中,我是否对Go工作区中的git存储库结构感到困惑?

I have 2 points of confusion about the How to Write Go Code article. They may be mistakes in the article, or I may just be missing the point. In describing the structure of the typical workspace, the article says

The src subdirectory typically contains multiple version control repositories (such as for Git or Mercurial) that track the development of one or more source packages.

The first example workspace in the article matches this description, with 2 folders representing repositories (github.com/golang/example/ and golang.org/x/image/), each of which has a .git directory immediately below it.

Later, the article says:

If you're using a source control system, now would be a good time to initialize a repository, add the files, and commit your first change. Again, this step is optional: you do not need to use source control to write Go code.

Then the article shows example code running git init in the src/github.com/user/hello/ directory. This is point of confusion #1. Since github.com/user is the repository and hello is the package, it seems like the git init should be done at the level of github.com/user.

Point of confusion #2 comes near the end of the article, when it says:

After issuing the above go get command, the workspace directory tree should now look like this:

It then shows another example repository, which has a .git directory under the github.com/golang/example/ directory, but none under the github.com/user/ directory, either right below it or in the hello package where it was initialized earlier.

My questions are:

  1. Should the repository have been initialized in the hello directory or one level up?
  2. Should the final repository show a .git directory?

The documentation you linked to does not show directory trees accurately - they combine directory names together which means directories of the same depth don't necessarily appear to have the same depth.

This is not the documentation being wrong - it's fairly common to flatten directory trees to avoid wasting screen-space (especially in Java projects where you'll have many directories all containing only a single subdirectory due to Java's file/package naming convention enforced by their compiler).

The tree diagram after "After issuing the above go get command, the workspace directory tree should now look like this:" should really look like this:

bin/
    hello                                   # command executable
src/
    github.com/
        golang/
            example/
                .git/                       # 
                hello/
                    hello.go                # 
                stringutil/                 #
                    reverse.go              # 
                    reverse_test.go         # 
        user/
            hello/                          # "github.com/user/hello" package repo root
                hello.go                    # 
            stringutil/                     # "stringutil" package source root, but not a repo
                reverse.go                  # 
                reverse_test.go             # test source

Note that github.com/user is described as a "base path" and not as a repository root or package path, but github.com/user/hello is described as a package root.

Should the repository have been initialized in the hello directory or one level up?

assuming hello directory is the root of your project, and that you are using a Version Control System, yes.

In the blog post, at https://golang.org/doc/code.html#Command, yes, this directory is the root of the hello program/project, belonging to the namespace github.com/user/. As a consequence, and because this demo uses a VCS, currently git, yes it contains a .git directory.

Should the final repository show a .git directory?

If this is a repository then yes, it contains a .git, or alike, directory to store metadatas of the VCS.

Although, having a repository, ie a directory managed by a VCS, is not mandatory to run go programs.

It is a requirement you have when you want to navigate over history, and / or save multiple state of a program, and / or write and share your code with other people.

In enterprise environment it is required (99.999999%, there must be some fools, we are only humans).

Since github.com/user is the repository and hello is the package, it seems like the git init should be done at the level of github.com/user.

No it isn't. github.com/user is a github user. Users have repositories, such as github.com/user/hello. A repository can hold one or more go packages, including one which may be at the top level of the repository, and have the same name as the repository.