Home  >  Article  >  Backend Development  >  What is the whitespace identifier in go language?

What is the whitespace identifier in go language?

青灯夜游
青灯夜游Original
2023-01-31 17:15:553477browse

In the Go language, whitespace identifiers are placeholders for unused values, represented by the underscore "_". A blank identifier can be used in the declaration or assignment of variables like other identifiers (any type can be assigned to it), but any values ​​assigned to this identifier will be discarded, so these values ​​cannot be used in subsequent code. , nor can you use "_" as a variable to assign values ​​or perform operations on other variables.

What is the whitespace identifier in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Like Python, the Go language also has whitespace identifiers.

What is a whitespace identifier

A whitespace identifier is a placeholder for an unused value. It is represented by an underscore (_). Because whitespace identifiers have no names, they are also called anonymous placeholders.

The whitespace identifier can be used in the declaration or assignment of variables like other identifiers (any type can be assigned to it), but any value assigned to this identifier will be discarded, so these values ​​cannot When used in subsequent code, _ cannot be used as a variable to assign values ​​or perform operations on other variables.

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. Likewise, if you import a package, it must also be used.

A blank identifier is needed at this time.

Unused assignment variables

If a function in Go returns multiple values, an equal number of variables must be defined to hold these values . However, if you only need some of the values ​​and not others, and if an assignment needs to match multiple lvalues ​​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 shown below:

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 whitespace identifier like this:

_, error = Dosomething()
if error {
   // handle error
}

Even if you declare a variable, you can use whitespace later identifier to ignore it, as shown in the following code:

product, error = Dosomething()
if error {
   // handle error
}
// ignore unused variable
_ = product

Use whitespace identifiers <pre class="brush:js;toolbar:false">func Sum(numbers []int) int { sum := 0 for _, number := range numbers { sum += number } return sum }</pre>

in for...range Unknown structure

can allocate anything using _:

var _ I = T{}

This variable cannot be accessed so that it will be optimized from the generated program. However, if the T type is not assigned to interface I, this may result in a compilation error. So in this case it is used as a static assertion about the type.

Unused packages

In Go, if you import a package, then you must use it, otherwise you will get a compiler error .

imported and not used “<package name>”

White space identifiers can be used to resolve this compiler error.

There are two ways to use blank identifiers to solve unused package errors, as follows:

First: declare a global blank identifier (before the main() function ), this identifier accesses symbols from unused packages, such as Open in the following code:

import "os"
var _ = os.Open
func main() {
}

Second: Add a blank identifier before the unused package, as follows:

import _ "os"

func main(){
}

White space identifiers make code more readable by avoiding unused variable declarations throughout the code.

Summary

Any package and variable declared but not used in Golang will throw an error. This rule keeps our code clean and lightweight.

• Sometimes you can use blank identifiers if you need to import some packages ahead of time for future use.

• Sometimes a function returns multiple parameters, but when we only need to use one of the variables, we can receive it through the blank identifier.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the whitespace identifier in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn