M
M
MDtox2018-05-31 11:24:10
go
MDtox, 2018-05-31 11:24:10

What does underscore mean in Go?

for _, v := range {
}
This construction is hard to understand, can you explain?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Pavlyuk, 2018-05-31
@pav5000

range by slice or map returns two variables, if you don't need the first one, write an underscore there. It means emptiness.

A
Anton Mashletov, 2018-05-31
@mashletov

Declared variables, according to the strict rules of Go, must be used (otherwise it will not compile). If this is not necessary (for example, when the function returns two values, and you only need the second), then we replace them with _ and there will be no more errors.

S
scranthony, 2020-03-22
@scranthony

What does it mean in import?
It says https://golang.org/ref/spec#Import_declarations:

An import declaration declares a dependency relation between the importing and imported package. It is illegal for a package to import itself, directly or indirectly, or to directly import a package without referring to any of its exported identifiers. To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:

That is, if with an underscore this is an explicit package connection, then what is the difference from a regular connection?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question