Lexical elements: Identifiers

January 12, 2023

Identifiers

Identifiers name program entities such as variables and types. An identifier is a sequence of one or more letters and digits. The first character in an identifier must be a letter.

identifier = letter { letter | unicode_digit } .

So in other words, every identifier must begin with a letter, followed by zero or more letters and/or digits. Pretty simple.

The spec offers a few examples

a
_x9
ThisVariableIsExported
αβ

That second one looks a bit suspicious. _x9? We were just told that the first character of an identifier has to be a letter!

Ah, but remember last week (if you were reading then), we read in the section on Letters and digits that:

The underscore character _ (U+005F) is considered a lowercase letter.

So now the reason for that seemingly odd exception should make more sense.

Some identifiers are predeclared.

And finally the section ends by telling us that some identifiers are pre-declared. We’ll get to those in due time, but as a sneak peak, this refers to names of basic data types, constants, nil, and a few built-in function names.

Quotes from The Go Programming Language Specification, Version of June 29, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Qualified identifiers

One of the features of Go I found most discomforting when I started using the language, was that of qualifiers. Depending on which previous languages you may have experience with, you may find them perfectly natural, or entirely strange. Let’s see what they are… Qualified identifiers A qualified identifier is an identifier qualified with a package name prefix. Both the package name and the identifier must not be blank. QualifiedIdent = PackageName ".


Uniqueness of identifiers

Uniqueness of identifiers Given a set of identifiers, an identifier is called unique if it is different from every other in the set. Two identifiers are different if they are spelled differently, or if they appear in different packages and are not exported. Otherwise, they are the same. This is pretty simple stuff, but it’s obviously worth being explicit. foo and bar are obviously different identifiers. Further, a in package foo, and a in package bar are different identifiers, because although they are spelled the same, they are not exported, and are in different packages.


Don't panic!

Later today, at 15:00 UTC, I’ll be joining Denis Čahuk and Adrian Stanek on their regular Livestream, Our Tech Journey, to talk about TDD, Go, and do some live coding. Join us! The second in our list of terminating statements is… panic! Terminating statements … A call to the built-in function panic. Don’t panic. Don’t panic is such ubiquitous Go advice that it’s one of the famous Go proverbs. So why do we have a built-in panic function if we’re not supposed to use it?