In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the reasons why GO does not support circular citation. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
One of them is the error report of the package's circular reference:
Package command-line-arguments imports github.com/eddycjy/awesome-project/an imports github.com/eddycjy/awesome-project/b imports github.com/eddycjy/awesome-project/a: import cycle not allowed
Suddenly very confused, why Go does not support circular references between packages, which is very puzzling, but also affect the performance?
As shown below:
Today, fried fish will work with you to understand the reasons behind it.
1. Case demonstration
Here we do a basic case Demo to facilitate students who have not come into contact with to establish a preliminary understanding. Our program has 2 package each.
The code for package an is as follows:
Import ("github.com/eddycjy/awesome-project/b") func Hello (s string) {b.Print (s)}
The code for package b is as follows:
Import ("fmt"github.com/eddycjy/awesome-project/a") func Hello () {a.Hello ("brain fried fish")} func Print (s string) {fmt.Println (s)}
Then call the a.Hello ("brain fried fish") method in the main.go file.
As soon as it is run, the following error message appears:
Package command-line-arguments
Imports github.com/eddycjy/awesome-project/a
Imports github.com/eddycjy/awesome-project/b
Imports github.com/eddycjy/awesome-project/a: import cycle not allowed
The essence of the error is that package a refers to package b, while package b refers to package a, resulting in circular references.
This is explicitly prohibited in the Go language, and the program will be interrupted during compilation, resulting in compilation failure.
2. Cause analysis.
According to the current Go official consensus, it is almost impossible to import package loops, and even Go2 has been explicitly rejected.
Because Go2 can be the key node of many core issues, many people have put forward proposals like "proposal: Go2: allow import cycle", hoping to solve the problem introduced by the loop.
Rob Pike, the father of the Go language, personally answered this question for the following reasons:
No support for circular references: the goal is to force Go programmers to think more about program dependencies.
Keep the dependency diagram concise.
Rapid program construction.
If circular references are supported: it can easily lead to laziness, poor dependency management, and slow build. This is what the designer does not want to see.
A chaotic dependency.
Slow program construction
So considering keeping the correct dependency graph in the first place DAG,Rob Pike thinks this is an area worth simplifying in advance.
It may be convenient to do an import loop in a Go program, but the cost behind it can be catastrophic, adversely affecting Go's build performance and dependencies.
So support is explicitly prohibited in Go.
This is the end of the article on "what are the reasons why GO does not support circular references?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.