1 min read
Logical operators
Logical operators Logical operators apply to boolean values and yield a result of the same type as the operands. The left operand is evaluated, and then the right if the condition requires it. && conditional AND p && q is "if p then q else false" || conditional OR p || q is "if p then true else q" ! NOT !p is "not p" I think this is pretty intuitive.
2 min read
Introducing Go 1.22!
Join me in less than an hour, when I’ll be live streaming again! I’ll be continuing to add SQLite support to Kivik, using Test-Driven Development. Go 1.22 was released on February 7. So I’m a bit behind on talking about the changes introduced to the spec. But better late than never! If you’re interested in my thoughts on some of the other changes, including those to the standard library, check out last week’s episode of the Cup o’ Go podcast.
2 min read
Comparison operators, conclusion
Comparison operators … Slice, map, and function types are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil. Comparison of pointer, channel, and interface values to nil is also allowed and follows from the general rules above. Let’s demonstrate: var a func() var b []int var c map[string]string fmt.Println(a == nil, b == nil, c == nil) // true, true, true But this only applies to a direct comparison with nil.
2 min read
Comparison operators, part IV
Comparison operators … Struct types are comparable if all their field types are comparable. Two struct values are equal if their corresponding non-blank field values are equal. The fields are compared in source order, and comparison stops as soon as two field values differ (or all fields have been compared). I think this is clear. But it leads to some interesting things in the wild, when you want structs to not be comparable.
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.
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.