In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use blank identifiers in Go". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Like Python, the Go language has blank identifiers.
1. What is a blank identifier
A blank identifier is a placeholder for an unused value. It is represented by an underscore (_). Because blank identifiers do not have a name, they are also called anonymous placeholders.
In the Go language, it is not allowed to declare unused variables or import statements. That is, we cannot declare a variable and leave it unused. Similarly, if you import a package, you must also use it.
At this point, a blank identifier is needed.
two。 Unused assignment variable
If a function in Go returns multiple values, you must define an equal number of variables to hold those values. However, if you only need some of these values and no other values, if an assignment needs to match multiple left values, but one of the variables will not be used by the program, use a blank identifier instead. This variable avoids creating useless variables and clearly indicates that the value will be discarded.
As follows:
Result, error = Dosomething () if error {/ / handle error}
In the above code, result has nothing to do with the program, and subsequent code does not use it. If compiled, the Go compiler will report an error.
Result declared but not used
In this case, you can replace the result with an underscore or a blank identifier
As follows:
_, error = Dosomething () if error {/ / handle error}
Even if you declare a variable, you can ignore it later using a blank identifier, as shown in the following code:
Product, error = Dosomething () if error {/ / handle error} / / ignore unused variable_ = product
Use white space markers in for...range
Func Sum (numbers [] int) int {sum: = 0for _, number: = range numbers {sum + = number} return sum} 3. Unknown structure
You can use _ to distribute anything:
Var _ I = T {}
Cannot access this variable so that it will be optimized from the generated program. However, if the T type is not assigned to interface I, it may result in a compilation error. Therefore, in this case, it is used as a static assertion about the type.
4. Unused package
In Go, if you import a package, you must use it, or a compiler error will occur.
Imported and not used ""
A blank identifier can be used to resolve this compiler error.
There are two ways to use blank identifiers to resolve unused package errors, as follows:
The first: declare a global blank identifier (before the main () function) that has never been used in the package access symbol, such as Open in the following code:
Import "os" var _ = os.Openfunc main () {}
The second: add a blank identifier to the unused package
As follows:
Import _ "os" func main () {}
Blank identifiers make the code more readable by avoiding the use of unused variable declarations throughout the code.
This is the end of the content of "how to use blank identifiers in Go". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.