2 min read
Variable types
Variables The static type (or just type) of a variable is the type given in its declaration, the type provided in the new call or composite literal, or the type of an element of a structured variable. And here we are at one of the core features of Go, it’s static type system. Each variable has a specific static type, which is typically provided in the variable definition, although there are a few cases where the type can be inferred.
1 min read
Variables and struct fields
Variables Structured variables of array, slice, and struct types have elements and fields that may be addressed individually. Each such element acts like a variable. This is pretty straight forward. In most contexts, the elements of a struct, array, or slice, can be treated as variables. They can be assigned, or assigned to, using the same syntax. A few examples to illustrate: type Person struct { ID int Name string Age *int } var p Person // p is an instance of the Person struct, with each field initially // set to its zero value.
2 min read
Variables
Last week Go 1.20 was released! It did include a few changes to the Go spec, but none of them affect the portions we’ve covered so far in this series, so we’ll just continue on our merry way… Variables A variable is a storage location for holding a value. The set of permissible values is determined by the variable’s type. A variable declaration or, for function parameters and results, the signature of a function declaration or function literal reserves storage for a named variable.
3 min read
Constants, typed vs untyped
Constants … Constants may be typed or untyped. Now that’s interesting. And powerful. This is one of the features of Go I featured in my recent video 10 Reasons I Like Go. This feature really helps bridge the gap between the convenience of dynamically typed languages, and the safety of statically typed languages. Let’s look at the details… Literal constants, true, false, iota, and certain constant expressions containing only untyped constant operands are untyped.
3 min read
Constants don't overflow
Constants … Numeric constants represent exact values of arbitrary precision and do not overflow. Consequently, there are no constants denoting the IEEE-754 negative zero, infinity, and not-a-number values. Now this is a very interesting thing about constants in Go, and it’s something that confuses a lot of people. So let’s play around with this one a bit. If you’re experienced with compiled languages, you’re probably already familiar with numeric overflows.
1 min read
Constants at compile time
Yesterday I made a passing comment in the last code example: Although x is not a constant, the result of len(x) is, because it is known at compile time. I want to dive into this aspect of constants in Go today. In Go (or at least most, if not all implementations of Go) a constant is logically replaced during compilation. This can be a useful mental shortcut when trying to determine whether a given expression can be a constant or not.
2 min read
Constants
Constants There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants. Rune, integer, floating-point, and complex constants are collectively called numeric constants. So there we have it. A comprehensive list of the available constant types in Go. And all but boolean and string constants are numeric. Notice that none of the composite types can be constants in Go. You can’t have a constant array, slice, struct, or interface.
Subscribe to Boldly Go: Daily
Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.
Unsure? Browse the archive.
3 min read
Lexical elements: Interpreted string literals
A quick note that episode 2 of Cup o’ Go dropped today. Check it out! Picking up where we left off last week, we’re discussing string literals. Last week we talked about raw string literals, which leaves us now with interpreted string literals. To recap: String literals A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.
3 min read
Lexical elements: Raw string literals
Now that I’ve bored you to death with more about rune literals than you ever asked, let’s take things up a level, and talk about strings. String literals A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals. Raw string literals are character sequences between back quotes, as in `foo`. Within the quotes, any character may appear except back quote.
3 min read
Lexical elements: Rune literals pt 3
Let’s continue our disection of rune literals. If you missed the parts, check them out from Monday when we discussed Unicode, and yesterday when we discussed quoting single characters. Today we’re looking at the various escape sequences supported by the rune literal syntax. Rune literals Several backslash escapes allow arbitrary values to be encoded as ASCII text. There are four ways to represent the integer value as a numeric constant: \x followed by exactly two hexadecimal digits; \u followed by exactly four hexadecimal digits; \U followed by exactly eight hexadecimal digits, and a plain backslash \ followed by exactly three octal digits.
2 min read
Lexical elements: Rune literals pt 2
Let’s continue our exploration of rune literals, which began on Monday. In summary from Monday, a rune in Go represents a Unicode code point. Continuing from there… Rune literals A rune literal is expressed as one or more characters enclosed in single quotes, as in 'x' or '\n'. Within the quotes, any character may appear except newline and unescaped single quote. A single quoted character represents the Unicode value of the character itself, while multi-character sequences beginning with a backslash encode values in various formats.