Objective
The go language is a very simple language, and it makes a lot of decisions for us, such as many of the coding styles that are not recommended in other languages are not allowed on go. For example, a variable or a package declaration or not used after import cannot be compiled. It's compiled only fatal without warning, which is good for code quality. Best practices in other languages are also code for writing Warning-free. Go puts this best practice at the language level.
empty indicator in Go (blank indentifier): _
Just beginning to see the empty designator in go is to think it's just a convention, because the underline looks more conspicuous than the ordinary variable is not likely to use only an underscore to name, in other languages we do not want to use a variable is very simple to ignore it, and if the return value of a function is not interested in words, Do not assign a value to the return value.
Note the conclusion: The null identifier is not a normal variable or an identifier, but rather a special identifier that does not really bind for this type of identifier-binding expression.
What do you mean by that? It's like that. Assigning a value to an empty operator is not bound to a value.
Package main
Import (
_ "IO"///If the name package is not the same as _ and in the code does not use this package will compile not through
//So import a package has side effects, import a package will execute the package init () method,
// It is not recommended to bind to _ just to avoid compilation.
"FMT"
)
Func Getmulti () (int, int) {return
3, 4
}
func main () {
_ = N/ /binding does not work, no error
//_: = 10//Edit The expression does not pass because there is not a valid new identifier//No on the left of the expressions
(variables) on side of: =
x, _: = Getmulti ()
FMT. Printf ("%d\n", x)
//FMT. Printf ("%d\n", _) is not compiled because _ cannot be assigned
// compiled "cannot use _ as value"
}
Summarize
A null identifier is not a normal identifier, it is a language-level identifier and is typically used to:
Explicitly ignores some of the return values in a function or other multivalued assignment expression, the multivalued expression usually has:
1, the function of multiple return values
2. Key-value value in range loop
3, multivalued assignment, such as: X, y, Z: = 10, 20, 30, but this situation is relatively small
Or the import package does not use the package and only uses the side effects of the package's initialization function. However, it is not recommended in this way to bypass problems that are not compiled because the package is not being used
This article here is just a note, in fact, the Go Language specification is written in very detailed. For some confused places, a manual will find the answer. This is also the advantage of a small language, ambiguity will be very few. I hope this article will help you learn the go language.