2 min read
Comparison operators, part III
Today we continue through the list of data types and how comparison and ordering works on each. Comparison operators … Channel types are comparable. Two channel values are equal if they were created by the same call to make or if both have value nil. Notice that channel comparison is effectively a comparison of identity. Not type or content. Two channels may have the same type and contents, but be unequal:
3 min read
Comparison operators, continued
Yesterday we started on comparison operators. Today we’ll continue, but there are a lot of types to cover, so I’ll break this down into a two or three parts, to keep each day’s email easily digestible. Recall that we had just been introduced to the concepts of ordered and comparable, and this is where we pick up… Comparison operators … These terms and the result of the comparisons are defined as follows:
2 min read
Comparison operators
I’ll be livestreaming again today! I hope you can join me as I continue where I left off last week, adding some new features to my open-source library, Kivik! Let’s talk about a mundane detail… that actually has some interesting nuances: Comparisons! Comparison operators Comparison operators compare two operands and yield an untyped boolean value. == equal != not equal < less <= less or equal > greater >= greater or equal In any comparison, the first operand must be assignable to the type of the second operand, or vice versa.
2 min read
String concatenation
Today we’re looking at a deceptively short section of the spec: String concatenation… String concatenation Strings can be concatenated using the + operator or the += assignment operator: s := "hi" + string(c) s += " and good bye" String addition creates a new string by concatenating the operands. That’s it. Short, and sweet, eh? Except that it’s not quite so sweet, when you consider the implications of the last sentence: Every time you use + or += for a string, you create a new string.
2 min read
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.
2 min read
Integer overflow
Integer overflow For unsigned integer values, the operations +, -, *, and << are computed modulo 2n, where n is the bit width of the unsigned integer’s type. Loosely speaking, these unsigned integer operations discard high bits upon overflow, and programs may rely on “wrap around”. Let’s illustrate with an example. I’ll use uint8, becuase it’s easier to reason about relatively small numbers, but the same holds for any of the uint family of types.
1 min read
Can you think of an use case for ^x?
Last week I talked about unary integer operators, including the ^ operator. A reader wrote back asking: Can you think of an use case for ^x? So today I’m going to answer this question! No! LOL Not very satisfying, is it? So I did some digging to find some examples. Note, none of these are my own examples. This is something most of us will never use. But it’s a good question nonetheless.
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.
21 min watch
FOSDEM 2024: You're already running my code in production
How I became a Go contributor, and you can, too.
2 min read
Unary integer operators
We have a quick one today to finish up integer operators, before diving into a semi-hairy topic next… Integer operators … For integer operands, the unary operators +, -, and ^ are defined as follows: +x is 0 + x -x negation is 0 - x ^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x and m = -1 for signed x So the first two are pretty obvious… + and - just allow you to specify the sign of a thing.
2 min read
Divide by zero, and shifting
Integer operators … If the divisor is a constant, it must not be zero. If the divisor is zero at run time, a run-time panic occurs. I’m sure you expected that. Division by zero is pretty universally not allowed. var a = 3 var b = 0 var c = a / 0 // Won't compile var d = a / b // run-time panic … If the dividend is non-negative and the divisor is a constant power of 2, the division may be replaced by a right shift, and computing the remainder may be replaced by a bitwise AND operation:
2 min read
Integer operators
Today we continue our discussion of arithmetic operators, with a topic that is likely not new to you at all: Integer operators. Integer operators For two integer values x and y, the integer quotient q = x / y and remainder r = x % y satisfy the following relationships: x = q*y + r and |r| < |y| This should be pretty obvious and intuitive… If you divide x / y and get a quotiont q and remainder r, then multiplying q * y and adding r should reveal x again.