Quidest?

Blank identifier in Go

· Lorenzo Drumond

The Go compiler won’t let us declare a variable that we never refer to again (it correctly infers that this must be a mistake)

Sometimes however we need a variable syntactically, as in the left-hand side of an assignment statement: in this case, we can use the blank identifier _ to act as a placeholder.

For example, if we’re only interested into knowing whether a key is present in a map or not, we can write

1_, ok := menu["eggs"]

References

#value #syntax #variable #map #programming #compiler #error #panic #golang #variables #blank #identifier