Lexical elements: Keywords, Operators and punctuation
January 13, 2023
Keywords
The following keywords are reserved and may not be used as identifiers.
break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var
Operators and punctuation
The following character sequences represent operators (including assignment operators) and punctuation:
+ & += &= && == != ( ) - | -= |= || < <= [ ] * ^ *= ^= <- > >= { } / << /= <<= ++ = := , ; % >> %= >>= -- ! ... . : &^ &^= ~
There’s not a lot to say about these rather dull lists of keywords and operators, except maybe to point out some conspicuous omissions.
One of the design goals for the Go programming language is simplicity. Both in terms of the language itself (i.e. a simple syntax), and in terms of usability (i.e. one way to do things). This has lead to some (in?)famous “corner cutting”, which often annoys many newcomers. Although I, and many other more seasoned Go developers, have come to appreciate many of these choices. Let’s look at a few.
-
while
How do you do an infinite loop without a
while
keyword? Why, with afor
loop with no closing condition! -
throw
andtry
/catch
Go doesn’t have exceptions, so there are no
try
/catch
keywords. Instead, it treats errors as first-class values, which, of course, we’ll be discussing in due time.Go also has
panic
, which looks a bit likethrow
, but has important differences we’ll get to. -
elseif
orelsif
No need for another keyword when we can just use the combination of the existing
else
andif
as inelse if
.
A large number of other concepts that are implemented as keywords in other languages also exist in Go, but via other mechanisms. For example, there’s no export
keyword in Go, because exported vs non-exported symbols are handled via capitalization. There’s no implements
keyword, because Go’s “Duck typing” provides other means to determine which types implement an interface. Etc.
Quotes from The Go Programming Language Specification, Version of June 29, 2022
Related Content

Empty structs
We finally we have enough knowledge for the EBNF format not to seem completely foreign, so let’s jump back and take a look at that, with the examples provided in the spec… Struct types … StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit . // An empty struct.

Struct tags
Struct types … A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored. struct { x, y float64 "" // an empty tag string is like an absent tag name string "any string is permitted as a tag" _ [4]byte "ceci n'est pas un champ de structure" } // A struct corresponding to a TimeStamp protocol buffer.

Struct method promotion
Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted. Struct types … Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows: If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.