2 min read
Conversions between numeric types
So we’ve gone through the high-level conversion stuff… now we dive into some particulars. Today, numeric conversions. Conversions between numeric types For the conversion of non-constant numeric values, the following rules apply: When converting between integer types, if the value is a signed integer, it is sign extended to implicit infinite precision; otherwise it is zero extended. It is then truncated to fit in the result type’s size. For example, if v := uint16(0x10F0), then uint32(int8(v)) == 0xFFFFFFF0.
2 min read
Type Conversion & struct tgs
We’ve already mentioned that struct tags are ignored when doing conversion between struct types. Now we see where that’s defined, along with an example: Conversions … Struct tags are ignored when comparing struct types for identity for the purpose of conversion: type Person struct { Name string Address *struct { Street string City string } } var data *struct { Name string `json:"name"` Address *struct { Street string `json:"street"` City string `json:"city"` } `json:"address"` } var person = (*Person)(data) // ignoring tags, the underlying types are identical I don’t think there’s much else to say about that, so let’s move on, and finish up this section.
2 min read
Conversion of type parameters
Today I’ll be live-streaming again, doing TDD on an open-source project. I hope you can join! Conversions … Additionally, if T or x’s type V are type parameters, x can also be converted to type T if one of the following conditions applies: Both V and T are type parameters and a value of each type in V’s type set can be converted to each type in T’s type set. Only V is a type parameter and a value of each type in V’s type set can be converted to T.
2 min read
Non-constant conversions, part 2
Today we’ll finish the list of non-constant conversion rules that don’t relate to type parameters. Conversions … x’s type and T are both integer or floating point types. x’s type and T are both complex types. x is an integer or a slice of bytes or runes and T is a string type. x is a string and T is a slice of bytes or runes. x is a slice, T is an array [Go 1.
2 min read
Non-constant conversions
Yesterday we looked at conversions of constants. Let’s now consider the more common conversion scenarios of variables and other non-constant expressions. Conversions … A non-constant value x can be converted to type T in any of these cases: x is assignable to T. ignoring struct tags (see below), x’s type and T are not type parameters but have identical underlying types. ignoring struct tags (see below), x’s type and T are pointer types that are not named types, and their pointer base types are not type parameters but have identical underlying types.
3 min read
Conversion of constants
In case you missed it… It has come to my attention that Monday’s livestream went to the wrong stream on YouTube! This means if you followed the link I shared with you on Monday, you likely sat there waiting for 2 hours, only to be disappointed by a lack of live programming. 😞 Alas, the livestream did happen, just at a different link by accident. So here’s that link, so you can watch the replay, if you so desire.
2 min read
Ambiguous conversion expressions
There aren’t many places where Go syntax is ambiguous, but here’s one: Conversions … If the type starts with the operator * or <-, or if the type starts with the keyword func and has no result list, it must be parenthesized when necessary to avoid ambiguity: *Point(p) // same as *(Point(p)) (*Point)(p) // p is converted to *Point <-chan int(c) // same as <-(chan int(c)) (<-chan int)(c) // c is converted to <-chan int func()(x) // function signature func() x (func())(x) // x is converted to func() (func() int)(x) // x is converted to func() int func() int(x) // x is converted to func() int (unambiguous) Of these three ambiguous cases, * is the one you’re most likely to run into, so let’s look at an example.
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
Conversions
On todays’s livestream, we’ll be Pair Programming on a real project. I hope to see you there! Conversions A conversion changes the type of an expression to the type specified by the conversion. A conversion may appear literally in the source, or it may be implied by the context in which an expression appears. An explicit conversion is an expression of the form T(x) where T is a type and x is an expression that can be converted to type T.
3 min read
Receive operator
Reminder! On Monday’s livestream, we’ll be Pair Programming on a real project. Don’t miss it! Channels are deceptively simple. They seem a bit magical. They feel like they do something. They feel like they send data around your system. But they really don’t. They’re just data types. They’re probably best thought of like Go’s array or slice type, with the limitation that you can only ever read the first value, and you can only ever set the last value.
1 min read
Live pair programming on a real project
Mark your calendar! If you’ve ever been curious about pair programming, and want to see it in action on a real project, not just a coding exercise, you’ll want to join Monday’s livestream. I’ll be doing some live Pair Programming with Denis Čahuk and Adrian Stanek. Denis and Adrian invited me to their livestream a couple of weeks ago, to talk about Test-Driven Development. You can watch that replay on YouTube.
2 min read
Address operators
Let’s talk about addresses. Address operators For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. This leads to one of my great annoyances about the Go language.