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 a for loop with no closing condition!

  • throw and try / 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 like throw, but has important differences we’ll get to.

  • elseif or elsif

    No need for another keyword when we can just use the combination of the existing else and if as in else 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


Share this

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

Unsure? Browse the archive .

Related Content


Floating-point operators

Floating-point operators For floating-point and complex numbers, +x is the same as x, while -x is the negation of x. The result of a floating-point or complex division by zero is not specified beyond the IEEE-754 standard; whether a run-time panic occurs is implementation-specific. I find this to be quite interesting. An implementation may choose to panic, or not, if you attempt to divide a floating-point or complex number by zero.


Arithmetic operators

Arithmetic operators Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (+, -, *, /) apply to integer, floating-point, and complex types; + also applies to strings. The bitwise logical and shift operators apply to integers only. + sum integers, floats, complex values, strings - difference integers, floats, complex values * product integers, floats, complex values / quotient integers, floats, complex values % remainder integers & bitwise AND integers | bitwise OR integers ^ bitwise XOR integers &^ bit clear (AND NOT) integers << left shift integer << integer >= 0 >> right shift integer >> integer >= 0 There should be nothing surprising here.


Operator precedence

I hope we all know what operator precedence means… but just in case it’s fuzzy, I’ll illustrate with a simple example from junior high school. What does this mean? 1 + 2 * 3 It’s either 9 or 7, right? It depends on the order in which we apply the + and * operations. I’m sure most of us agree that the correct answer is actually 7, because multiplication takes precedence over addition.