Detour: Unary operators & signed numeric literals
January 19, 2023
I noticed something while writing the last two sections on Integer literals and Floating-point literals, and I’m curious if anyone else noticed.
There’s no way to express a negative integer or floating-point literal!
If we look specifically at the definition of an decimal literal in EBNF format, we see:
decimal_lit = "0" | ( "1" … "9" ) [ [ "_" ] decimal_digits ] .
Notably, we do not see this:
decimal_lit = [ "+" | "-" ] "0" | ( "1" … "9" ) [ [ "_" ] decimal_digits ] .
Does this mean Go doesn’t support negative numbers? Well, obviously not. What use is a programming language without negative numbers?
Does it mean it doesn’t allow assigning negative values directly to constants or variables? No, that’s easily disproven as well.
So what gives?
The +
and -
signs are interpteted by Go as Unary operators (there are a few others as well).
So what that means is when you write
var myInt = -30
You have not written a negative integer literal (-30
). Rather, you’ve written an integer literal (30
), preceeded by the unary operator -
which negates it value.
Remember that one for your next Go Pub Quiz night!
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.