This can be very frustrating whenever the Go program is compiled with loop introduction errors.

So why doesn’t Go allow loops to be introduced?

I think the reasons are as follows:

Find out where the package is located

First, understand the positioning of package in Go language;

Go’s packa is the same concept as libraries and modules in other languages. In other languages, implementing a library or module requires creating a “separate project”, whereas in Go, a single package is sufficient. In normal Coding, we can introduce foreign projects (for example, PHP projects introduce PHP packages), but can we modify the packages introduced at will? Can’t be! When we write PHP, can we import packages from outside and make changes in the imported packages, and rely on existing projects in a loop? Let alone!

From this perspective, it makes sense that the Go language doesn’t allow loops because a Package in Go is equivalent to “a small project” in other languages.

Language design level

Second, let’s consider the possible disadvantages of circular introduction.

Rob Pike, author of the Go language, has been suggested to remove loop introductions in later versions of Go; Rob Pike strongly disagrees. Rob Pike felt that if you have a problem with loops between two packages, it’s because you didn’t think about module division at the beginning of the design.

We just think, if allowed to cycle introduced, then the existed between module and call each other, along with the advancement of the project, more and more the dependencies between modules, resulting in two modules coupling is high, has become increasingly blurred the boundaries between the change of initial module, finally are coupled together, become a mess. A good design, a good partition of modules, should not exist the problem of cyclic dependency!

As a result, the Go language is designed to prevent loops from being introduced, which forces developers to think about module-to-module dependencies and keep them clean before writing code. Otherwise, allowing cyclic introduction brings convenience to coding, but from the long-term perspective of the project, it is very detrimental to the construction of the whole project and the cleanliness of the code.

Other reasons

Finally, disallowing loops will make compilation more efficient.

For more exciting content, please follow my wechat official accountInternet Technology Nest