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.


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.


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.


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.

How-Tos

21 min watch


FOSDEM 2024: You're already running my code in production

How I became a Go contributor, and you can, too.


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.


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:

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.


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.

Other

1 min read


Join me at FOSDEM

Do you have plans to be in Brussels this weekend? If so, join me at FOSDEM 2024! I’ll be speaking in the Go Devroom on Saturday morning on my journey to becoming a Go contributor. Not going to be in Brussels? Follow me on Mastodon where I’ll be live micro-blogging from the event, and stick around for links to a video of my presentation when it’s made available.


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.